package main import "fmt" func main() { var a any a = 1024 fmt.Println(a) // 1024 a = "从入门到放弃" fmt.Println(a) // 从入门到放弃 a = true fmt.Println(a) // true }
package main import "fmt" func main() { var a any = 3.14 // 断言空接口类型变量a为string类型(断言失败) var tmpStr, _ = a.(string) fmt.Printf("tmpStr = %+v \n", tmpStr) // tmpStr = // 断言空接口类型变量a为int类型(断言失败) var tmpInt, _ = a.(int) fmt.Printf("tmpInt = %+v \n", tmpInt) // tmpInt = 0 // 断言空接口类型变量a为float64类型(断言成功) var tmpFloat64, _ = a.(float64) fmt.Printf("tmpFloat64 = %+v \n", tmpFloat64) // tmpFloat64 = 3.14 }
Copyright © 2024 码农人生. All Rights Reserved