Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Introduction to GoLang generics and advanced usage

  sonic0002        2022-12-17 05:12:21       4,176        2    

Generics in Go allow you to write code that can work with multiple types of data, without having to write separate versions of the code for each type. This can make your code more flexible and easier to maintain, as you only need to write and test the code once, rather than maintaining multiple versions.

To use generics in Go, you first need to define a type parameter, which is a placeholder for the type that the code will work with. For example, you might define a type parameter called "T" like this:

func MyFunction(x T) T {
  // code here
}

You can then use the type parameter "T" wherever you would normally use a type in the function. For example, you could define a variable of type "T" like this:

func MyFunction(x T) T {
  var y T
  // code here
}

To use the function with a specific type, you can then specify the type when calling the function. For example, if you want to use the function with integers, you would call it like this:

result := MyFunction(5)

You can also use generics with multiple type parameters. For example, you might define a function that takes two type parameters like this:

func MyFunction(x T, y U) T {
  // code here
}

In this case, you would specify both types when calling the function. For example:

result := MyFunction(5, "hello")

There are a few restrictions on the use of generics in Go. For example, you cannot use them with types that are not comparable or types that do not have a specific zero value. You also cannot use them with functions that have a variadic parameter or a return value with a named type.

Go does not have native support for generics, so they are implemented using code generation. This means that the code you write using generics is transformed into specific versions of the code for each type that you use it with. This can be a powerful tool, but it also means that the generated code can be difficult to read and understand.

One advanced use of generics in Go is to create reusable code that can work with multiple types of data. For example, you might create a generic "sort" function that can sort any type of data that implements the "Less" method:

func Sort(data []T) {
  for i := 0; i < len(data)-1; i++ {
    for j := i + 1; j < len(data); j++ {
      if data[i].Less(data[j]) {
        data[i], data[j] = data[j], data[i]
      }
    }
  }
}

To use this function, you would need to create a type that implements the "Less" method, like this:

type MyType struct {
  x int
}

func (t MyType) Less(other MyType) bool {
  return t.x < other.x
}

You can then use the "Sort" function to sort a slice of "MyType" values like this:

data := []MyType{
  {x: 5},
  {x: 2},
  {x: 8},
  {x: 1},
}

Sort(data)

This will sort the slice of "MyType" values based on the value of the "x" field, using the "Less" method as the comparison function.

Another advanced use of generics in Go is to create type-safe code that can work with multiple types of data, but still enforce certain constraints on the types that can be used. For example, you might create a generic "Sum" function that can add any two values of the same type, as long as the type implements the "Add" method:

func Sum(x, y T) T {
  return x.Add(y)
}

To use this function, you would need to create a type that implements the "Add" method, like this:

type MyType struct {
  x int
}

func (t MyType) Add(other MyType) MyType {
  return MyType{x: t.x + other.x}
}

You can then use the "Sum" function to add two values of type "MyType" like this:

x := MyType{x: 5}
y := MyType{x: 2}
result := Sum(x, y)

This will return a value of type "MyType" with the sum of the two values' "x" fields.

I hope this gives you a better understanding of some advanced uses of generics in Go. Let me know if you have any questions or need further clarification.

GOLANG  GENERICS 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  2 COMMENTS


Marcelloh [Reply]@ 2022-12-29 03:20:21

This is very well written and explains the subject in a very understandable way.

Ke Pi [Reply]@ 2022-12-30 12:08:18

Thx a lot for the praise.