我想將 struct 的指針傳遞給期望 interface{} 的函數。然后(通過反射)獲取指向結構成員的指針,然后使用此指針修改它。我已經閱讀了很多問答并嘗試了很多變化,但我仍然可以讓它工作。讓我們考慮下面的例子:type Robot struct { Id int}f := func(i interface {}) { v := reflect.ValueOf(i).Elem().FieldByName("Id") ptr := v.Addr().Pointer() *ptr = 100 //^ it needs to me for functions expecting the pointer: Scan(&pointerToValue)}robot := &Robot{}f(robot)println(robot.Id) //I want to get here 100我認為問題在于對反射包的Addr()和Pointer()方法實際上做了什么了解不深..
如何從接口獲取結構體成員的指針{}
慕的地8271018
2021-09-10 21:28:47