Golang & Concurrency (50 Qs)•Part 1: Go Language Core, Memory & Runtime Internals (1 - 15)
Pointers in Go: Pass-by-Value Semantics and `unsafe.Pointer`.
senior LevelgolangGo Language Core
In Simple Words: Everything in Go is passed by value:
- When you pass an
int, the integer is copied. - When you pass a pointer (
*User), the 8-byte pointer address is copied. - When you pass a slice, map, or channel, the small header struct is copied (pointing to the shared underlying data).
- Go does not support pointer arithmetic (
ptr++) unless explicitly bypassing type safety viaunsafe.Pointeranduintptr.