【发布时间】:2016-10-14 23:06:57
【问题描述】:
我对 interface{} 类型感到困惑,
如何从 Person 结构构建 interface{} 对象?
如果结构很大,转换成本是否昂贵
type Person struct {
name string
age int
}
func test(any interface{}) {
}
func main() {
p := Person{"test", 11}
// how to build an interface{} object from person struct?
// what is the cost? the field need copy?
test(p)
}
【问题讨论】:
-
经验法则:如果您不了解
interface{},请不要使用它。 -
其实我也不想用,但是很多库函数的输入参数是interface{}类型