Skip to content

Instantly share code, notes, and snippets.

@xiocode
Created April 26, 2013 16:22
Show Gist options
  • Save xiocode/5468519 to your computer and use it in GitHub Desktop.
Save xiocode/5468519 to your computer and use it in GitHub Desktop.
根据 struct 的 一个 filed 获取 struct 的指针!
import (
"fmt"
"unsafe"
)
type A struct {
data string
e int
}
func main() {
a := &A{"abc", 1234}
e := &(a.e)
offset := uintptr(unsafe.Offsetof(A{}.e))
base := uintptr(unsafe.Pointer(e)) - offset
x := (*A)(unsafe.Pointer(base))
fmt.Printf("%p , %p, %#v\n", a, x, x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment