Created
February 25, 2021 14:51
-
-
Save yurakawa/3d0bc39df974e0b321c00ebb8ed9c214 to your computer and use it in GitHub Desktop.
interface slice
This file contains 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
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