Canonical guidance

Use when

Avoid

Preferred pattern

type header struct {
	Len  uint32
	Kind uint16
}

func readHeader(p unsafe.Pointer) header {
	// p must point to at least unsafe.Sizeof(header{}) bytes of valid memory.
	return *(*header)(p)
}

Anti-pattern

Explanation: This anti-pattern is tempting in hot paths, but once assumptions about layout and lifetime are implicit the code becomes fragile across change.

Why

Sources