反射怎么通过地址修改结构体中的值

yz23je · · 4566 次点击
```go func StudentData(params interface{}) { if *(*uint8)(unsafe.Add(*(*unsafe.Pointer)(unsafe.Pointer(&params)), 8<<(^uint(0)>>63)+4+3))&32 == 0 { panic("readonly!") } base := *(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&params), 4<<(^uint(0)>>63))) newid, newname, newage := 1, "小小", 18 *(*int)(unsafe.Add(base, 0)) = newid *(*unsafe.Pointer)(unsafe.Add(base, 4<<(^uint(0)>>63))) = *(*unsafe.Pointer)(unsafe.Pointer(&newname)) *(*int)(unsafe.Add(base, 4<<(^uint(0)>>63)*2)) = len(newname) *(*int)(unsafe.Add(base, 4<<(^uint(0)>>63)*3)) = newage } ```
#5
更多评论
需要先Elem然后才能修改
#1
其实是我结构体成员没有大写,尴尬了
#2