Introduction to Go 1.18 Generics

What are Generics? Generic programming is a style or paradigm of computer programming. Generics allow programmers to write code using types that are specified later, which are then instantiated when the code is used. Basic Usage of Generics in Golang Examples Map Operation package main import ( "fmt" ) func mapFunc[T any, M any](a []T, f func(T) M) []M { n := make([]M, len(a), cap(a)) for i, e := range a { n[i] = f(e) } return n } func main() { vi := []int{1, 2, 3, 4, 5, 6} vs := mapFunc(vi, func(v int) string { return "<" + fmt.Sprint(v * v) + ">" }) fmt.Println(vs) } Min Max Functions ...

March 16, 2022 · zhoukuncheng