Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Part 1: Types, Zero Values & Pointers
GO

Part 1: Types, Zero Values & Pointers

Revise the Go type system, zero values, pointers versus values, structs, runes, and essential language fundamentals.

1. The type system

  • Statically typed — every variable has a fixed type; no implicit conversions between numeric types.
  • Short declaration := — infer type, must declare at least one new var.
  • var with explicit type — also fine; var x int = 5.
  • Multiple declaration / assignment: a, b := 1, 2; parallel assignment a, b = b, a swaps without temp.
  • Untyped constants — adopt the context type when used (x := 5 → int; f := 5.0 → float64).
  • Interfaces are the exception: they can hold any concrete type.

2. Zero values

Every declared variable starts at its zero value — no null in Go:

TypeZero value
int, float0 / 0.0
boolfalse
string""
pointer / interface / map / slice / func / channelnil
structall fields zeroed

Interview point: zero values make Go memory zero-initialized — safe to use without explicit setup.

3. Pointers vs values

  • &x → address; *p → dereference.
  • Pointers cannot point to nothing; nil pointer dereference panics.
  • A pointer *int differs from a value int in the type system — method receivers and interfaces care.
x := 10
p := &x
*p = 20
fmt.Println(x, *p)   // 20 20
  • When to use a pointer: mutate caller data, share large structs, indicate optional/missing.
  • Slices/maps are reference-like already — copying a slice header copies the pointer.

4. Structs

  • Fields, tags, embedding.
  • Struct comparison — comparable only between identical types; never across structs.
  • A struct with slices/maps inside is not comparable with == (only comparable to nil).
  • Method on struct: value receiver vs pointer receiver (see Part 2).
type Point struct{ X, Y int }
a := Point{1, 2}
b := Point{1, 2}
fmt.Println(a == b)   // true — all fields equal
  • Unexported fields (lowercase) are private to the package; export with capital first letter.
  • %+v shows field names; empty struct struct{} (aka struct{}{}) is zero-size, useful as signal in channels.

4. Runes

  • string is a byte slice — on the UTF-8 encoding of Unicode.
  • rune = int32 — one Unicode code point.
  • len(s) counts bytes, not Runes; utf8.RuneCountInString for character count.
  • for range over a string iterates runes, not bytes.
  • 'A' is a rune literal; "A" is a string.
s := "héllo"
fmt.Println(len(s))                       // 6 bytes
fmt.Println(len([]rune(s)))               // 5 runes

Gotcha: indexing/slicing a string operates on bytes — slicing mid-rune can produce invalid UTF-8.

5. Interview checkpoint

  • Zero value vs nil vs empty.
  • &/* semantics; pointer vs value copy.
  • Rune vs byte; len() byte vs rune count.
  • Escape analysis / heap vs stack allocation and where pointers commonly live.
  • Struct value vs pointer slices — when each is idiomatic.
  • Untyped constants in numeric expressions.

My Private Notes

Notes are auto-saved locally to this device.