First try of Generics types in Go
The long wanted Generics feature is finally available in Go 1.18!
package main
import "fmt"
type NumberOrString interface {
int64 | string
}
func Swap[V NumberOrString](a, b V) (V, V) {
return b, a
}
func main() {
var n1, n2 int64 = 123, 456
n3, n4 := Swap(n1, n2)
fmt.Println(n3, n4)
var s1, s2 string = "Mike", "Lin"
s3, s4 := Swap(s1, s2)
fmt.Println(s3, s4)
}
And the Go Playground link. Will dig more into the new feature and I believe that it will be so fun!!! Also check the official tutorial if you can't wait.