Variables
A variable is a named storage location with a fixed type. You declare it with var, or with := when the compiler can infer the type from the value on the right.
Detailed Explanation
You need a place to keep a port number while you parse flags and then start a server. In Go that place has a name, a type, and a value that starts as the type's zero if you do not assign one. The type does not change later. var port int is an int for the rest of its scope; you cannot store a string in it.
Two declaration forms. var name Type = value is the long form, valid at package level and inside functions. var name Type omits the value and uses the zero. var name = value infers the type. Inside a function you can also write name := value, which declares and assigns in one step. := is illegal at package level; package-level names always use var or const.
Types are explicit even when inferred. n := 0 is an int, not a flexible number. n := int64(0) is an int64. n := 0.0 is a float64. Inference uses the default type of the untyped constant on the right. If that default is not what you wanted, write the type.
Scope is block-shaped. A name declared inside { lives until the matching }. Function parameters are in scope for the whole function. Package-level names are in scope for the whole package, including other files. There is no global keyword. A nested declaration with the same name shadows the outer one; the outer name is not overwritten.
Assignment is not declaration. port = 8080 requires port to already exist. port := 8080 declares it. Mixing them is a common compile error: := when the name already exists in that block, or = when it does not. The special case is x, err := f() when x is new and err already exists in the same block: := reuses err and declares x. At least one name on the left of := must be new.
Exported variables. A package-level var Timeout = 5 * time.Second that starts with a capital letter is part of your API. Callers can read and write it. Mutable exported package state is a source of races; prefer a function that returns a value, or document that the variable is meant to be set before any goroutine starts.
What it costs. A variable is a few bytes of stack or a word in the data segment. The expensive part is the value it holds: a large struct copied on assignment, a slice header that shares a backing array. Name the type you mean, and keep mutable package-level variables rare.
Code Examples
package main
import "fmt"
var defaultPort = 8080 // package level: var, not :=
func main() {
var port int // 0
port = defaultPort
host := "localhost" // declared and inferred as string
fmt.Println(host, port)
}func parse(raw string) (int, error) {
var n int
// n already exists, so assign with = after Atoi.
parsed, err := atoi(raw)
if err != nil {
return 0, err
}
n = parsed
return n, nil
}
func atoi(s string) (int, error) { return 0, nil }func shadow() {
n := 1
{
n := 2 // new n, outer n is unchanged
fmt.Println(n) // 2
}
fmt.Println(n) // 1
}Common Gotchas
Shadowing hides the outer variable
an inner `n :=` does not update the outer `n`. This is legal and a frequent source of "I assigned it and the caller still sees zero."
Unused local variables are a compile error
a declared name you never read fails the build. Assign to `_` if you must discard a value.