How Do You Use errors.Is and errors.As in Go?
errors.Is matches a value in a wrapped error chain and errors.As extracts a typed error. See both with %w wrapping, examples and pitfalls.
587 posts about Go: questions with answers, tutorials and articles, newest first.
errors.Is matches a value in a wrapped error chain and errors.As extracts a typed error. See both with %w wrapping, examples and pitfalls.
A sentinel error is a named, comparable error value like io.EOF. See how to declare, wrap and test them, and what they cost your public API.
Learn the Go guideline to accept interfaces and return structs, why it keeps APIs flexible and testable, and the cases where you should break it.
Understand fmt.Stringer, io.Reader and io.Writer, the three small standard-library interfaces behind printing, files, networks and buffers in Go.
An interface is nil only when both its type and value are nil. See why a nil pointer inside an error is not nil and how to avoid the trap.
The empty interface any can hold a value of any type. Learn how it relates to interface{}, how to assert types safely and when generics are better.
An empty struct in Go has no fields and takes zero bytes. See why it is used for sets, done channels and stateless types, and where to avoid it.
The functional options pattern configures a Go constructor with small option functions. See how it works, how to validate options and when to choose it.
Go has no constructors, so the NewXxx function convention fills the gap. See how to write one, validate input and decide between pointers and values.
A nil pointer dereference panic means your code followed a pointer that points to nothing. Learn the causes, how to read the trace and how to prevent it.
Learn when Go structs can be compared with == and used as map keys, which field types prevent it, and how to compare structs that hold slices.
Struct tags attach metadata to Go fields. Learn the syntax, common json options, how reflect reads tags, and why malformed tags fail silently.
Convert between strings and numbers in Go with strconv: Atoi, Itoa, ParseInt, ParseFloat, ParseBool and Format functions, plus correct error handling.
Use strings.Builder to build strings in loops without repeated copying. Covers WriteString, Grow, io.Writer use, and pitfalls such as copying a builder.
Check whether a key exists in a Go map with the comma ok idiom, remove entries with delete, and avoid nil map and zero value pitfalls.
Learn how to sort slices in Go with the slices package: ordered types, structs, multiple fields, stable sorts, and how it compares with the older sort package.
Go has no built-in remove. Use slices.Delete, the append idiom or a swap with the last element, and know what each does to the backing array.
A slice is a pointer, length and capacity over an array, so copies and sub-slices share data. See when append breaks the link and how to avoid bugs.
Create slices with literals and make, grow them with append and copy them into independent slices, with length, capacity and common mistakes.
The init function runs automatically once per package, before main. Learn the exact order, the rules, real uses and the downsides of init.