最佳答案
下面是一个不起作用的简单应对程序:
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
fmt.Println(getProperty(&v, "X"))
}
func getProperty(v *Vertex, property string) (string) {
return v[property]
}
错误:
Go: 18: 无效操作: v [ property ](类型为 * Vertex 的索引)
我想要的是访问顶点 X 属性使用它的名称。如果我做 v.X
,它工作,但 v["X"]
不。
有人能告诉我怎么做吗?