Last active
June 11, 2018 08:42
-
-
Save guozengxin/86db3fe0bac8afff1a825e36c1b85e77 to your computer and use it in GitHub Desktop.
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
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