我還處于理解 Go 接口的早期階段。我正在編寫一些邏輯模擬,并具有類似以下代碼的內(nèi)容(我在這里進行了大量簡化):請參閱我的問題的評論:type LogicNode struct { Input *bool Output *bool Operator string Next Node}func (n *LogicNode) Run() { // do some stuff here n.Next.Run()}type Node interface { Run()}func main() { nodes := make([]Node, 1000) for i := 0; i < 1000; i++ { n := LogicNode{ //assign values etc. } nodes[i] = &n } for i, node := range nodes { // I need to access LogicNode's Output pointer here, as a *bool. // so I can set the same address to other Node's Input thereby "connecting" them. // but I could only get something like this: x := reflect.ValueOf(node).Elem().FieldByName("Output") // which is <*bool Value> // I couldn't find a way to set a new *bool to the underlying (LogicNode) Struct's Input or Output.. }}我使用接口的原因是因為還有其他節(jié)點類型 FloatNode MathNode 等,它們具有不同的字段,但它們實現(xiàn)了自己的 run 方法。我已經(jīng)成功地使用了 Value 的 SetString 或 SetBool 方法,但無法在那里設(shè)置指針......提前致謝。
- 1 回答
- 0 關(guān)注
- 249 瀏覽
添加回答
舉報
0/150
提交
取消