short variable declaration
The := form that declares a new variable and sets its value in one step, letting Go infer the type.
In more detail
It is the form you will use most inside functions, as in pct, err := usage(200, 164). It is not allowed at package level, where you must use var. On the left side at least one name must be new, which lets err be reused.
Example
host := "db-1"Related terms
- var declaration — The Go statement that declares a variable with the var keyword, optionally giving its type and a starting value.
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- multiple return values — A Go function returning more than one result at once, most often a useful value together with an error.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Errors as Values — Concurrent Systems Programming with Go