Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Created February 13, 2021 14:20
Show Gist options
  • Save romanitalian/d5e5faadef0dba027de3a27b5a4e1dad to your computer and use it in GitHub Desktop.
Save romanitalian/d5e5faadef0dba027de3a27b5a4e1dad to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"unsafe"
)
type Foo struct {
aaa [2]bool // offset of bytes: 0
bbb int32 // offset of bytes: 4
ccc [2]bool // offset of bytes: 8
}
type Bar struct {
aaa [2]bool // offset of bytes: 0
ccc [2]bool // offset of bytes: 2
bbb int32 // offset of bytes: 4
}
func main() {
ff := Foo{}
bb := Bar{}
fmt.Printf("offsets of fields: aaa: %+v; bbb: %+v; ccc: %+v\n", unsafe.Offsetof(ff.aaa), unsafe.Offsetof(ff.bbb), unsafe.Offsetof(ff.ccc))
fmt.Printf("offsets of fields: aaa: %+v; ccc: %+v; bbb: %+v\n", unsafe.Offsetof(bb.aaa), unsafe.Offsetof(bb.ccc), unsafe.Offsetof(bb.bbb))
}
// go run main.go
//
// offsets of fields: aaa: 0; bbb: 4; ccc: 8
// offsets of fields: aaa: 0; ccc: 2; bbb: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment