liminfo

Go Reference

Free reference guide: Go Reference

40 results

About Go Reference

The Go Language Reference is a comprehensive cheat sheet covering the essential syntax and standard library patterns of the Go programming language. It is organized into eight categories that mirror Go's unique design: Basics (package, import, var, :=, const/iota, fmt, if/else, for, switch, defer), Types (array, slice, make, map, string, pointer), Functions (func, multiple returns, named returns, variadic, closures), Structs (struct, methods, embedding, tags), Interfaces (interface, empty interface, type assertion, type switch, error), Goroutines (go, sync.WaitGroup, sync.Mutex), Channels (chan, directional channels, select, range channel), and Packages (go mod, errors, context, generics).

Go is widely used for backend services, cloud-native applications, CLI tools, and distributed systems. This reference is valuable for Go developers at all levels — from beginners learning the := short declaration and defer mechanics, to experienced engineers looking up the exact syntax for bidirectional channel direction constraints or errors.As/errors.Is wrapping patterns. The reference includes bilingual KO/EN support and renders with syntax highlighting so code examples are immediately readable.

Each entry provides the precise syntax token or keyword, a concise description of its behavior, and a multi-line code example demonstrating real-world usage. The reference pays particular attention to Go-specific idioms that differ from other languages: iota-based enum patterns, goroutine lifecycle management with WaitGroup, channel multiplexing with select, and Go 1.18+ generics with type constraints. The context package pattern for deadline propagation is also included, reflecting modern production Go code.

Key Features

  • Variable declaration syntax: var vs := short declaration, const with iota for enum-like patterns
  • Complete slice mechanics: make(), append(), cap(), len(), and sub-slice expressions like nums[1:3]
  • Map operations: literal initialization, delete(), safe key lookup with the comma-ok idiom
  • Struct definition with value/pointer receiver methods, embedding for composition, and JSON struct tags
  • Interface types including empty interface (any), type assertion, type switch, and the error interface
  • Goroutine patterns: go keyword, sync.WaitGroup for coordination, sync.Mutex for safe shared state
  • Channel patterns: buffered vs unbuffered channels, directional chan<-/(<-chan) constraints, select multiplexing, and range over closed channels
  • Go 1.18+ generics syntax with type parameters and constraints.Ordered, plus context.WithTimeout for deadline management

Frequently Asked Questions

What is the difference between var and := in Go?

var is a full variable declaration that can be used at package scope or inside functions, and allows explicit type annotation (e.g., var name string = "Kim"). The := short declaration is only available inside functions and infers the type automatically (e.g., count := 42). Both are shown in the reference with examples.

How does Go handle multiple return values?

Go functions can return multiple values natively, e.g., func divide(a, b float64) (float64, error). The caller receives both with result, err := divide(10, 3). This pattern is the standard idiom for error handling in Go, replacing exceptions used in other languages.

What is the difference between a buffered and unbuffered channel?

An unbuffered channel (make(chan int)) blocks the sender until a receiver is ready, providing synchronization. A buffered channel (make(chan int, 5)) allows up to N sends without a receiver, enabling asynchronous communication up to the buffer capacity. The select statement lets you multiplex over multiple channels.

How does defer work in Go?

defer schedules a function call to run just before the surrounding function returns. It is commonly used for cleanup: defer f.Close() ensures a file is always closed even if the function exits early due to an error. Multiple defers execute in LIFO (last-in, first-out) order.

What are goroutines and how do they differ from threads?

Goroutines are lightweight user-space coroutines managed by the Go runtime, not the OS. They start with just a few KB of stack and scale to thousands in a single process. You launch one with the go keyword: go myFunc(). Use sync.WaitGroup to wait for goroutines to complete.

How does Go implement interfaces without explicit declaration?

Go uses structural (implicit) typing: a type satisfies an interface if it implements all the required methods, without any "implements" keyword. For example, if your Circle type has Area() float64 and Perimeter() float64 methods, it automatically satisfies the Shape interface. This enables duck-typing-style polymorphism.

What are Go generics and when should I use them?

Go 1.18 introduced generics (type parameters), allowing functions and types to work over multiple types. Example: func Min[T constraints.Ordered](a, b T) T works for both int and string. Use generics to eliminate code duplication in data structures and utility functions without sacrificing type safety.

How does error wrapping work with errors.Is and errors.As?

fmt.Errorf("wrap: %w", err) wraps an error, preserving the chain. errors.Is(wrappedErr, target) checks if any error in the chain matches the target value. errors.As(wrappedErr, &appErr) checks if any error in the chain matches the target type and sets the pointer. This is Go's idiomatic structured error handling.