go语言中空接口有如下三种主要用法
package main
import "fmt"
type Course struct {
name string
price int
url string
}
func print(i interface{}) {
fmt.Printf("%V\n", i)
}
func main() {
//空接口
var i interface{} //空接口
//空接口类似与python的object
//空接口:任意一个类型都赋给空接口变量
i = Course{}
print(i)
fmt.Println(i)
i = 10
print(i)
i = "bobby"
print(i)
i = []string{"math", "chinese", "english"}
print(i)
//2.参数传递
//3.空接口可以作为map的值
var teacherInfo = make(map[string]string)
teacherInfo["name"] = "wwwjsssb"
teacherInfo["age"] = "11"
//使用空接口方法
var teacherInfoInterface = make(map[string]interface{})
teacherInfoInterface["age"] = 12
fmt.Printf("%v\n", teacherInfoInterface)
}