More Standard Library Essentials·Q532·Medium
You implement heap.Interface (which embeds sort.Interface , so Len , Less and Swap , plus Push(any) and Pop() any ). Then you call the package functions heap.Init ,…
More Standard Library Essentials·Q533·Medium
Stable: sort.SliceStable , sort.Stable and slices.SortStableFunc . Not stable: sort.Slice , sort.Sort , slices.Sort and slices.SortFunc , which all use pdqsort since Go…
More Standard Library Essentials·Q534·Hard
You write a layout by showing how the reference time 01/02 03:04:05PM '06 -0700 would look in your format: 1=month, 2=day, 3/15=hour, 4=minute, 5=second, 6=year, 7=zone.…
More Standard Library Essentials·Q535·Medium
math/rand and math/rand/v2 are fast pseudo-random generators whose output can be predicted . crypto/rand reads from the OS CSPRNG (getrandom, BCryptGenRandom and so on).…
More Standard Library Essentials·Q536·Hard
The two packages share the same syntax. text/template does no escaping . html/template parses the HTML around each action and works out its context : element body,…
More Standard Library Essentials·Q537·Medium
Always pass user values as arguments to QueryContext , ExecContext or QueryRowContext , and never build SQL with fmt.Sprintf or + . The driver sends the query and the…
More Standard Library Essentials·Q538·Hard
== and bytes.Equal return as soon as they hit the first byte that differs. By measuring response times over many requests, an attacker can recover a token or HMAC byte…
More Standard Library Essentials·Q539·Hard
Go's regexp uses RE2 syntax and automata-based matching (a Thompson NFA, a one-pass matcher and a bounded backtracker). It guarantees matching in O(m·n) time , linear in…
More Standard Library Essentials·Q540·Hard
exec.Command(name, args...) runs the program directly (execve or CreateProcess). No shell is involved, so ; , | , $() and globs inside arguments are just literal bytes.…
More Standard Library Essentials·Q541·Hard
The pattern: Call Accept in a loop. Start one goroutine per connection. Set deadlines on every read and write so that slow or idle clients cannot pin goroutines forever.…
More Standard Library Essentials·Q542·Medium
fs.FS (Go 1.16) is a minimal read-only interface: Open(name string) (fs.File, error) . Optional interfaces add capabilities: ReadDirFS , ReadFileFS , StatFS , SubFS and…
More Standard Library Essentials·Q543·Hard
os.OpenRoot(dir) returns an *os.Root . All of its operations ( Open , Create , OpenFile , Mkdir , Remove , Stat , Lstat , plus ReadFile , WriteFile , MkdirAll , Rename ,…
More Standard Library Essentials·Q544·Medium
You define flags ( flag.Int , flag.StringVar , flag.Func , flag.TextVar , flag.BoolFunc ), call flag.Parse() once in main , and read flag.Args() for the positional…
More Standard Library Essentials·Q545·Medium
strconv is type-specific and does not allocate for parsing. It returns precise errors and is several times faster than fmt.Sprintf("%d") or fmt.Sscan , which go through…
More Standard Library Essentials·Q546·Hard
encoding/binary: fixed-layout binary protocols and file formats (network headers, on-disk records, hashing inputs) where you control every byte. There is no schema and…
More Standard Library Essentials·Q547·Medium
The runtime receives OS signals and, for each signal you registered with signal.Notify(ch, sigs...) , forwards it to your channel instead of doing the default action…