【发布时间】:2017-09-20 05:59:23
【问题描述】:
例如,我可以从接口 Work 中获取其底层类型的零值吗?
func MakeSomething(w Worker){
w.Work()
//can I get a zeor value type of The type underlying w?
//I tried as followed, but failed
copy :=w
v := reflect.ValueOf(©)
fm :=v.Elem()
modified :=reflect.Zero(fm.Type())//fm.type is Worker, and modified comes to be nil
fm.Set(modified)
fmt.Println(copy)
}
type Worker interface {
Work()
}
【问题讨论】:
标签: go reflection