Interface空接口万能类型与类型断言机制
package mainimport "fmt"// interface{}是万能数据类型
func myFunc(arg interface{}) {fmt.Println("myFunc is celled....")fmt.Println(arg)// interface{} 该如何区分 此时引用的底层数据类型到底是什么?// 给interface{} 提供"类型断言"的机制value, ok := arg.(string)if !ok {fmt.Println("arg is not string type")} else {fmt.Println("arg is string type, value = ", value)fmt.Printf("value type is %T\n", value)}fmt.Println("===================")
}type Books struct {auth string
}func main() {book := Books{"张三"}myFunc(book)myFunc(100)myFunc("abc")myFunc(3.14)
}