Generics in Go

Generics in Go

You probably know that Go is a statically typed language, which means that every variable in your program must have a predefined type. For example, when we define a function, we need to specify the types of its parameters, like in the following code: func Print(s string) { fmt.Println(s) } Here, we have defined a parameter named “s” with the string type. We can define any number of additional parameters with any types, such as float64, int, or structs. However, the problem arises when we want to pass a number(or other types) to this function because numbers are of type int, and we can’t assign an int to a variable of type string. Previously, we used to define the type of the “s” parameter as an interface, which partially solved this problem but still had many limitations. ...

August 27, 2024 · 3 min