Golang Generics is Officially HERE!! (Full Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's finally time yes go generics is officially here with us according to the official go developer survey the lack of a generics in go has been the most frustrating part of a writing goal programs by far and in today's video i'll show you some real gold code with a generics on a real ide not on a go beta playground but on a real ide because of the blessing of the most recent official release of a 1.18 i truly believe that this is the missing piece for go to receive even more industry adoption in the coming years all right let's get started and write some code with a go generics [Music] something to yourself welcome back into the golang dojo your number one resource for all things go make sure to get your free yo link cheat sheet shoot at golangdojo.com cheat sheet in today's video we are talking about generics now we all know that things like variables that can be parameters we can pass in variables into a function for example and that function can take that variable and perform further operations within that function and the magical thing about generics is that it allows types to be parameters as well to use the generics in go you would have to be on a go version 1.18 and above there are three main features in gold genomics now we're gonna dive into exactly how they work in just a moment but that the three main features are type parameter with constraint the type inference and tie set next let's go through an example at first without go generics integrated and then incrementally introduce these three features let's say that we have there's a mint in the function that will return the smaller of the two integers that being passed in so we do the comparison and return the minimum value of the two integers passed in in the main function we will print out the result let's go ahead and run this program we will see one being printed out because one is the smaller one of the two by the same token we can write a main function for float 64 as well and pass in 0.1 and 0.2 if we run this program again we will see 0.1 being printed out because 0.1 is the smaller one out of 0.1 and 0.2 however what if we want to keep on adding different types maybe we want to add a string type for example or just add the different types of integers into 8 in 64 and so on it would be pretty silly to keep on adding functions even though the only change to the function is just the types wouldn't it be nice if we can have a general main function and have the type to be decided by the caller of this main function to be decided by the type of the parameters being passed in this is where generics comes into play and more specifically type a parameter comes into play we can add a type parameter by providing it a pair of a square bracket and give it a general type and give it a constraint for now we will just use interface which indicates any type is a fair game here we have the type parameter being declared here we have the type constraint being declared which indicates the available types are there's a type parameter and then you can use this type parameter to indicate the type of the parameters that being passed in and the results that you are returning with there's a general main function with a generics integrated we can now write both of these instructions with only one function implementation the way that you call this a main function is pretty much the exact same thing as you would calling the original min main enter function excel so now you need to utilize these square brackets and provide the type for this main function and we can do the exact same thing with flow sexy form as you can see we are not seeing the ide returning any compiling error if we go ahead and run this program again we can see 1 and 0.1 are being printed out as expected now i am aware that this is not the correct logic for the main function but for now i want to focus on the type of constraint now remember i mentioned the interface a keyword right here imposes no constraint for the type and any type is a fair type for the type parameter declared right here because of this attribute go actually introduced a new keyword called any which does the exact same thing indicating any type is if a fair type for the type of parameter declared right here if you want to impose a summer type constraint then you can change this to an actual type such as a flow 64. now you can only pass in parameters that are of a type of flow 64. and you can only return a flow 64 value as well let's go ahead and comment out the instruction where we're passing into as the type of model because that is not going to work anymore because an end is not a flow 60 form let's go ahead and run this program again we will see still being printed out so that is about the first feature of a generics aka type parameter along with a type of constraint next let's talk about a type inference type inference is pretty straightforward it simply gives us the capability to infer the type of the generic function so instead of having to provide this a type right here as the type parameter with these square brackets and we can actually remove it and the ide is not going to give us any compiling error and if we were to run this program again we will still get the exact same result however there is one caveat that i want to point out let's say that we have an additional type call superfloat and the underlying type of this superflow type is flow 64. let's go ahead and instantiate a variable as f of a type superflow assign it a float 64 value now if we try to call this a main function again with the sf of a type superflow instead go ahead and run this program again it's actually not going to work two things i want to mention here the very first one is this is still a compile time error if we try to compile this program using a different goal command go build main.go instead it will still give me a compile time error i think the reason the id is not giving me a syntax a slash compile time error is that the id is not yet up to date with the latest ago features secondly as you can probably already observe the message is suggesting that we are possibly missing a tilde sign if we scroll down right here now we have a constraint of flow 64 but this is not going to accept any other type unless it is a flow 64. it's not it going to even accept any type even though the underlying type of that type is a flow 64. in order for this a constraint to accept any type as long as the underlying type is of this type we need to add a tilde sign in the beginning of it if you compile this program again no error message will be shown and if we run this program again we would be able to see 0.3 which is as of the value of sf right here being printed out so that is about type inference let's next let's talk about type set now as we mentioned previously we can use any to indicate that there's not any constraint for the type parameter or we can give it a type as the constraint for the type parameter on one end with the keyword any you are giving the type parameter with unlimited options and on the other hand you are giving the type parameter with a specific type which is a very limited whatever you want something in between you don't want to take in just about any types you also don't want to be super limited you want something in between that you want a list a set of types under consideration and this is where typeset comes into play let's say that we have two criterias for the main function right here the very first one is i want to take in any types as long as the types underlying type is flow 64. so this type constraint right here the second criteria for the type constraint is i also want to take in int we can declare such a set of type constraints or typeset using interfaces to do that we will need to declare a new type let's call it min types of a type interface between the curly brackets we will put in tilde flow 64 and the end right here combined with the or operator right here this will be the typeset we are replacing here in the type constraint and now we can use this amine to accept ends as well as any types as long as the types underlying type is a flow 64. so let's go ahead and run this program again as you can see everything is running smoothly now you might be thinking um i still don't quite forget the point of a typeset what is the reason that we can't take in just about any types and we have to limit ourselves into this set of types right here that is because every type has its own attributes and some types share the same attributes versus others don't remember we don't have our main function fully implemented the logic is not yet implemented if we were to copy the real logic of the main function and paste it here as you can see there is a compiling error in the compiling message it says the operator smaller than is not defined on this type on any because not every type is an ordered type it is a comparable type that can utilize the smaller than operator on the contrary main types are right here which includes end and includes any type whose underlying type is float 64 is a comparable type as a result if we replace the any with main types right here as you can see the compiling error goes away and if we run this program again as you can see everything is working perfectly fine again and as you may have noticed the difference here we are seeing 0.2 instead of a 0.3 that is because sf right here is of value 0.3 and 0.2 is the smaller out of the two which is what this main function is supposed to return so that is about how generics works in go now depending on your background generics it may or may not be the most natural concept to you so please feel free to go over this video more than once if there's any point within the video that you couldn't quite grasp within the very first one that said please give this video a nice little thumbs up subscribe to the channel and make sure to get your free golang cheat sheets at golangdojo.com and i'll see you ninjas in the very next video [Music] you
Info
Channel: Golang Dojo
Views: 30,624
Rating: undefined out of 5
Keywords: golang, learn golang, go lang, go language, golang tutorial, go programming, golang for beginners, golang 2022, golang generics, golang generics. do we actually need them?, generics in golang, generics in go, generics in golang explained, golang tutorial for beginners, generics in go programming language, generics implementation in golang, generics, golang in 2022, golang crash course, go programming language, Go 1.18, Golang 1.18, Go version 1.18
Id: SJ9rvg5nT2c
Channel Id: undefined
Length: 13min 30sec (810 seconds)
Published: Wed Mar 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.