Skip to content

Go — Basics

  • Static typed, compiled, garbage collected, opinionated formatting (gofmt).
  • Zero values: every type has one — 0, "", false, nil. No null vs undefined confusion.
  • No classes — structs + methods + interfaces. No inheritance, only composition.
  • Multiple return values: idiomatic for (value, err).
  • Capitalization = export. Foo exported, foo package-private.
  • Primitives: int, int64, uint, float64, bool, string, byte (= uint8), rune (= int32).
  • Composite: array (fixed size), slice (dynamic), map, struct, channel, function, interface, pointer.
  • string is immutable byte sequence; range over string yields runes (UTF-8 decoded).
  • Array: [3]int — fixed size, value type.
  • Slice: []int — header (ptr, len, cap). Reference type. append may reallocate.
  • make([]int, len, cap).
  • Slicing shares underlying array — beware of memory retention.
  • map[K]V. Not safe for concurrent write — use sync.Map or mutex.
  • Iteration order is randomized.
  • Zero value of map is nil — must make() before write.
type User struct {
ID string
Email string
}
func (u User) IsAdmin() bool { return u.ID == "admin" } // value receiver
func (u *User) Rename(n string) { u.Email = n } // pointer receiver
  • Use pointer receiver if method mutates or struct is large.
  • Embedding for composition: type Admin struct { User; Permissions []string }.
  • Implicit satisfaction — no implements.
  • Empty interface interface{} (or any since 1.18) holds anything.
  • Small interfaces preferred (io.Reader = single method).
  • No exceptions. Functions return (T, error). Caller checks: if err != nil { return err }.
  • Wrap with fmt.Errorf("ctx: %w", err). Unwrap with errors.Is, errors.As.
  • panic only for unrecoverable; recover inside deferred fn.
  • go fn() — launch goroutine (lightweight, ~2KB stack, grows).
  • Channel: typed conduit. ch := make(chan int, 5) (5 = buffer; 0 = unbuffered/sync).
  • Send: ch <- v. Receive: v := <-ch. Close: close(ch).
  • Range until closed: for v := range ch { ... }.
  • Select: multi-channel wait.
select {
case v := <-ch1:
case ch2 <- x:
case <-time.After(1*time.Second):
case <-ctx.Done():
}
  • sync.Mutex (write lock), sync.RWMutex (read-write).
  • sync.WaitGroup — wait for N goroutines.
  • sync.Once — run-once init.
  • sync.Pool — object pool, GC-friendly.
  • sync/atomic — lock-free counters / loads / stores.
  • context.Context propagates cancellation, deadline, request values.
  • context.Background() root, context.TODO() placeholder.
  • context.WithCancel, WithTimeout, WithDeadline, WithValue.
  • Always pass as first arg: func F(ctx context.Context, ...).
  • Always defer cancel().
func Map[T, U any](xs []T, f func(T) U) []U {
out := make([]U, len(xs))
for i, x := range xs { out[i] = f(x) }
return out
}
  • Type parameters with constraints (comparable, ~int, custom interface).
  • go mod init, go get, go mod tidy.
  • go build, go run, go test, go vet, go fmt.
  • Cross-compile: GOOS=linux GOARCH=amd64 go build.
  • Build tags: //go:build linux.
  • net/http, encoding/json, database/sql, os, io, fmt, time, regexp, sort, strings, bytes, bufio.