Standard library / unsafe
unsafe.Offsetof()
Reports the byte offset of a selected struct field from the beginning of its struct. The selector must identify a field reachable without indirect pointer-embedded traversal. Padding can change the offset. Prefer ordinary field selection. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Offsetof(x ArbitraryType) uintptrParameters & Attributes
| Name | Type | Description |
|---|---|---|
| x | ArbitraryType | Struct field selector, not an arbitrary standalone expression. |
Return Value
uintptr
Field offset in bytes.
Working Example
Example Snippetgo
package main
import ( "fmt" "reflect" "unsafe")
func main() { s := struct { A byte B uint32 }{1, 2} field, _ := reflect.TypeOf(s).FieldByName("B") fmt.Println(unsafe.Offsetof(s.B) == field.Offset)}