Skip to content

Instantly share code, notes, and snippets.

@yurakawa
Created February 25, 2021 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurakawa/3d0bc39df974e0b321c00ebb8ed9c214 to your computer and use it in GitHub Desktop.
Save yurakawa/3d0bc39df974e0b321c00ebb8ed9c214 to your computer and use it in GitHub Desktop.
interface slice
package main
import "fmt"
// https://github.com/golang/go/wiki/InterfaceSlice
func main() {
stringSlice := []string{"Tokyo", "Saitama"}
// var i interface {} = stringSlice // OK
// var is []interface{} = stringSlice // NG
is := make([]interface{}, 0, len(stringSlice))
for _, s := range stringSlice {
is = append(is, s)
}
fmt.Println(is)
// [Tokyo Saitama]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment