Loading...
Loading…
Loading…
Categories
Popular tags
Syntax, concurrency, error handling, and CLI reference
// Declaration
var x int = 10
x := 10 // short declaration (inside functions)
const Pi = 3.14
// Multiple assignment
a, b := 1, 2
a, b = b, a // swap
// Zero values: 0, false, "", nil// Basic
func add(a, b int) int { return a + b }
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 { return 0, errors.New("division by zero") }
return a / b, nil
}
// Named return values
func minMax(a, b int) (min, max int) {
if a < b { return a, b }
return b, a
}
// Variadic
func sum(nums ...int) int {
total := 0
for _, n := range nums { total += n }
return total
}type User struct {
ID int
Name string
Age int
}
// Value receiver (copy)
func (u User) String() string {
return fmt.Sprintf("%s (%d)", u.Name, u.Age)
}
// Pointer receiver (mutates)
func (u *User) Birthday() { u.Age++ }
// Embedding
type Admin struct {
User
Role string
}type Writer interface {
Write(p []byte) (n int, err error)
}
// Implicit implementation — no "implements" keyword
type FileWriter struct{ f *os.File }
func (fw FileWriter) Write(p []byte) (int, error) {
return fw.f.Write(p)
}
// Empty interface
func printAny(v interface{}) { fmt.Println(v) }
// or in Go 1.18+:
func printAny(v any) { fmt.Println(v) }
// Type assertion
if w, ok := v.(Writer); ok { w.Write(data) }
// Type switch
switch t := v.(type) {
case int: fmt.Println("int:", t)
case string: fmt.Println("string:", t)
}// Standard pattern
result, err := doSomething()
if err != nil {
return fmt.Errorf("context: %w", err)
}
// Sentinel errors
var ErrNotFound = errors.New("not found")
if errors.Is(err, ErrNotFound) { /* ... */ }
// Custom error type
type ValidationError struct { Field, Msg string }
func (e *ValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Field, e.Msg)
}
var ve *ValidationError
if errors.As(err, &ve) { fmt.Println(ve.Field) }// Launch goroutine
go func() { fmt.Println("async") }()
// Unbuffered channel (synchronises sender and receiver)
ch := make(chan int)
go func() { ch <- 42 }()
val := <-ch
// Buffered channel
ch := make(chan int, 10)
// Select — multiplex channels
select {
case msg := <-ch1: fmt.Println("ch1:", msg)
case msg := <-ch2: fmt.Println("ch2:", msg)
case <-time.After(1 * time.Second): fmt.Println("timeout")
}
// Close and range over channel
close(ch)
for v := range ch { fmt.Println(v) }// Mutex
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
// RWMutex
var rw sync.RWMutex
rw.RLock(); defer rw.RUnlock() // read
rw.Lock(); defer rw.Unlock() // write
// WaitGroup
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
work(id)
}(i)
}
wg.Wait()
// sync.Once
var once sync.Once
once.Do(func() { /* runs exactly once */ })// With timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// With cancellation
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-someSignal
cancel()
}()
// Pass context through call chain
func fetchUser(ctx context.Context, id int) (*User, error) {
return db.QueryContext(ctx, "SELECT * FROM users WHERE id=$1", id)
}
// Check if context is done
select {
case <-ctx.Done():
return ctx.Err()
default:
}go run main.go # run without building
go build ./... # build all packages
go test ./... # run all tests
go test -race ./... # run with race detector
go test -cover ./... # show coverage
go mod tidy # clean up go.mod/go.sum
go mod download # download dependencies
go vet ./... # static analysis
go fmt ./... # format code
go doc fmt.Println # show docs for a symbol
gopls # language server (IDE)