1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package main import "fmt"
func main() { fmt.Println("") var i, j, k interface{} name := []string{"yasuo", "yongen"} i = name fmt.Println("i behalf of arr:", i) i2 := 20 age := i2 j = age fmt.Println("j behalf of num:", j) str := "hello world" k = str fmt.Println("k behalf of string:", k) value, ok := k.(int) if !ok { fmt.Println("type is not int") } else { fmt.Println("key value is", value) } arr := make([]interface{}, 3) arr[0] = 10086 arr[1] = "yasuo" arr[2] = true for _, v := range arr { switch v := v.(type) { case int: fmt.Println("type is int ,value is", v) case bool: fmt.Println("type is bool ,value is", v) case string: fmt.Println("type is string ,value is", v) default: fmt.Println("type is not find") } } }
|