Grokking Advanced Go
0% completed
Generic Methods
The module below has one small program, and its go.mod file names Go 1.26.
module example go 1.26
package main import "fmt" type Page[T any] struct{ Items []T } func (p Page[T]) Map[U any](f func(T) U) Page[U] { var result Page[U] for _, v := range p.Items { result.Items = append(result.Items, f(v)) } return result } func main() { cents := Page[int64]{Items: []int64{2500, 1250}} fmt.Println(cents.Map(func(c int64) string { return fmt.Sprint(c / 100) })) }
go build .
# example ./main.go:7:22: generic method requires go1
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%