#go Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Go Type Switches
When you have an `interface{}` (or any) and need to dispatch on its underlying type, use `switch v := x.(type)`. Cleaner than a chain of type-assertions.
Go Custom Generic Constraints
Type constraints are interfaces with type sets. Use them to express "any numeric" or "any ordered" without resorting to `any`.
Go Interface Segregation — Small Interfaces
"The bigger the interface, the weaker the abstraction." Go encourages small, focused interfaces (often just one method) — io.Reader, io.Writer, fmt.Stringer. Define them at the consumer side.
Go time.Format — The Reference Time
Go's time formatting uses a reference date instead of strftime tokens: `Mon Jan 2 15:04:05 MST 2006` (which is 01/02 03:04:05PM '06 -0700, or 1/2/3/4/5/6/7 — mnemonic). Strange at first, instantly memorable.
Go sql.NullString — Nullable DB Columns
Plain `string` can't represent SQL NULL. Use `sql.NullString` (and its siblings `NullInt64`, `NullBool`, etc.) for columns where NULL is a real value distinct from empty.
Go strconv — String ↔ Number Conversions
`strconv.Atoi` / `Itoa` for the common int<->string case; `ParseFloat`, `ParseBool`, `Quote` for everything else. Always handle the error — strings can be anything.
Go database/sql — Transaction Helper
Wrap your transaction in a function that commits on success and rolls back on error or panic. Drop-in: pass any `func(*sql.Tx) error` and forget about manual cleanup.
Go iota — Auto-Incrementing Constants (Enums)
Go has no `enum` keyword but `iota` inside a const block gives you the same effect. Each line is the previous expression with iota auto-incremented.
Go strings package essentials
The most-reached-for functions in the `strings` package: Split, Contains, TrimSpace, ToLower/Upper, Replace, Index, HasPrefix/Suffix.
Go time.Ticker — Periodic Tasks
`time.NewTicker(d)` fires on a channel every `d` interval. Combine with select + a done channel for a clean way to run periodic work that can be cancelled.
Go text/template — Simple Templating
Stdlib templating with `{{.Field}}` placeholders, range loops, if/else, and function pipelines. Safe alternative for non-HTML text (e.g. emails, config files).
Go strings.Builder — Efficient Concatenation
Building strings with `+` allocates O(n²) memory. `strings.Builder` writes to an internal buffer with a single final allocation — orders of magnitude faster in loops.
Go time.Since — Easy Benchmark Timing
`time.Since(start)` is shorthand for `time.Now().Sub(start)`. Pair with `defer` for a quick "how long did this function take?" measurement.
Go Benchmarks with testing.B
`BenchmarkXxx(b *testing.B)` functions are run by `go test -bench`. `b.N` is the loop count the framework auto-tunes; report results in ns/op, allocs/op.
Go Generic Map / Filter / Reduce
Three classic functional combinators, generically typed. Go's standard library doesn't ship these (yet), but they're short enough to drop into any project.
Go json.RawMessage — Deferred Decoding
`json.RawMessage` is a `[]byte` that survives a decode pass. Use it for envelope-style JSON where one field's type depends on another (e.g. `{"type":"X","payload":{...}}`).
Go JSON API Endpoint
Decode an incoming JSON body into a typed struct, do work, encode a JSON response. The whole roundtrip is ~20 lines of standard library.
Go Read File — Whole / Lines / Bytes
Three common patterns: load it all (`os.ReadFile`), iterate line-by-line with `bufio.Scanner`, or stream chunks via `io.Reader`. Pick by file size.