package main import ( "fmt" ) type S1 struct {i int; j int} func typeName(v interface{}) string { switch v.(type) { case int: return "int" case string: return "string" case *S1: return "S1" default: return "unknown" } } func typeName2(v interface{}) string { kk := 2 switch i := v.(type) { case int: assert(i + kk == 890) return "int" case string: return "string" case *S1: _ = i return "S1" case []int: return "[]int" case map[string]int: assert(i["a"] == 666) return "map[string]int" case map[string][]int: return "map[string][]int" default: return "unknown" } return "int" } func test_ts_in_fmt() { var nums = []int{2, 5, 1, 3, 4, 7} fmt.Println(nums) } func main() { var s *S1; re := typeName(s) assert(re == "S1") re = typeName2(888) re2 := typeName2([]int{1}) re3 := typeName2(map[string]int{"a":666}) re4 := typeName2(map[string][]int{"a":{1}}) re5 := typeName2([]float32{1.2}) fmt.Println("typeswitch", re, re2, re3, re4, re5) assert(re == "int") assert(re5 == "unknown") test_ts_in_fmt() }