我有 2 个不同的 API 版本,我正在尝试重构代码以避免重复。在我的方法中,我可以接收来自 V1 或 V2 的对象(它们具有相同的属性)。我想访问这些属性,目前我收到以下错误:i.name undefined (type interface {} is interface with no methods)package mainimport "fmt"type V1 struct { name string age int address string}type V2 struct { name string age int }func main() { v1 := &V1{name: "Susan", age: 15} describe(v1) v2 := &V2{name: "John", age: 21} describe(v2) }func describe(i interface{}) error { fmt.Printf("(%v, %T)\n", i, i) switch v := i.(type) { default: return fmt.Errorf("Error detected, unexpected type %T", v) case *V1: fmt.Println("*V1") i = i.(*V1) case *V2: fmt.Println("*V2") i = i.(*V2) } fmt.Println(i.name) return nil}有什么建议么?
- 2 回答
- 0 关注
- 139 浏览
添加回答
举报
0/150
提交
取消
