Introduction to Go
Demo Creator
@seed-creator · muallif
Why Go?
Go compiles to a single static binary, has a minimal runtime, and ships with a built-in concurrency model (goroutines + channels) that makes network services trivial to parallelize.
Go's simplicity is a feature: there is usually one obvious way to do something, which makes reading unfamiliar code fast.
Goroutines and Channels
A goroutine is a lightweight thread managed by the Go runtime. Channels are typed conduits that goroutines use to communicate without sharing memory directly.
Do not communicate by sharing memory; share memory by communicating. Channels make ownership explicit and eliminate a class of race conditions.
Error Handling
Go returns errors as values. Functions return (Result, error) tuples; callers check the error explicitly. This makes the error path visible in the code rather than hidden in exception stacks.
func fetchUser(id string) (*User, error) { resp, err := http.Get(apiURL + "/users/" + id) if err != nil { return nil, fmt.Errorf("fetch user %s: %w", id, err) } defer resp.Body.Close() var user User if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { return nil, fmt.Errorf("decode user: %w", err) } return &user, nil}
Wrap errors with context using %w so callers can unwrap with errors.Is/As.
The Standard Library
Go ships with batteries included: net/http for servers, encoding/json for JSON, database/sql for DB access, and testing for unit tests. Most services need very few third-party dependencies.
0 ta izoh
Tizimga kiring izoh qoldirish uchun.