Last active
June 3, 2021 15:09
-
-
Save betandr/2c14e59d103badf726789a688962025c to your computer and use it in GitHub Desktop.
Go 2 Generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Go 2 Playground: https://go2goplay.golang.org/ | |
// | |
// Or to install the go2go tool: | |
// git clone https://go.googlesource.com/go goroot | |
// cd ./goroot | |
// git checkout dev.go2go | |
// cd ./src | |
// CGO_ENABLED=0 ./all.bash | |
// export PATH=/path/to/your/goroot/bin:$PATH | |
// | |
// go tool go2go run ./main.go2 | |
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
func main() { | |
ss := []string{"hello", "generic", "world"} | |
is := []int{0, 3, 7} | |
printStringSlice(ss) | |
printIntSlice(is) | |
printCase(ss) | |
printCase(is) | |
printReflect(ss) | |
printReflect(is) | |
printGeneric(ss) | |
printGeneric(is) | |
} | |
// A specific implementation for slices of strings | |
func printStringSlice(list []string) { | |
fmt.Println("string slice: ") | |
for _, s := range list { | |
fmt.Print(s, " ") | |
} | |
fmt.Print("\n") | |
} | |
// A specific implementation for slices of integers | |
func printIntSlice(list []int) { | |
fmt.Println("int slice: ") | |
for _, i := range list { | |
fmt.Print(i, " ") | |
} | |
fmt.Print("\n") | |
} | |
// An implementation based on a nil interface supporting either slices of integers or strings using a case statement | |
func printCase(v interface{}) { | |
fmt.Println("case: ") | |
switch list := v.(type) { | |
case []string: | |
for _, s := range list { | |
fmt.Print(s, " ") | |
} | |
case []int: | |
for _, i := range list { | |
fmt.Print(i, " ") | |
} | |
} | |
fmt.Print("\n") | |
} | |
// A implementation based on a nil interface supporting slices using reflection | |
func printReflect(v interface{}) { | |
fmt.Println("reflect: ") | |
val := reflect.ValueOf(v) | |
if val.Kind() != reflect.Slice { | |
return | |
} | |
for i := 0; i < val.Len(); i++ { | |
fmt.Print(val.Index(i).Interface(), " ") | |
} | |
fmt.Print("\n") | |
} | |
// An implementation using Go generics supporting any slice of type []T | |
func printGeneric[T any](slice []T) { | |
fmt.Println("generic: ") | |
for _, v := range slice { | |
fmt.Print(v, " ") | |
} | |
fmt.Print("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment