Skip to content

Instantly share code, notes, and snippets.

@guozengxin
Last active June 11, 2018 08:42
Show Gist options
  • Save guozengxin/86db3fe0bac8afff1a825e36c1b85e77 to your computer and use it in GitHub Desktop.
Save guozengxin/86db3fe0bac8afff1a825e36c1b85e77 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
func switchFunc(c interface{}) {
v := reflect.ValueOf(c)
switch v.Kind() {
case reflect.String:
fmt.Println("string", v.String())
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
fmt.Println("slice", i, v.Index(i))
}
default:
fmt.Println("default", v)
}
}
func main() {
a := []string{
"郭富成",
"古天乐",
"刘德华",
}
var b []interface{}
for k, v := range a {
fmt.Println(k, v)
b = append(b, v)
}
var c interface{} = b
switchFunc(c)
ss := "郭富成, 郭富2"
switchFunc(ss)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment