From the course: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Generics - Go Tutorial
From the course: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
Generics
- [Instructor] During the recording of this course, Go 1.18 came up. One of the biggest changes that comes with Go 1.18 is generics. Let's see what it means. In generics, an interface can also be a set of types. So I'm saying that the ordered interface is either an integer, a float 64, or a string. And now I can use this type in my function. So I'm writing a minimum function, which has a type T, which is a generic type, with the constraint that it is ordered. And from here, we continue as usual. We're saying that minimum gets a slice of T and return of T is single value and a possible error. First we check if the length of value is zero, we need to return the zero value for T and an error. So we define a viable zero T and then return it and an error saying minimum of an empty slice. If there are values, we start with the initial values then do a follow, compare, and assign to the minimum value. And finally, we turn the…