:= (short declaration)
Try it yourself →Defines a variable and automatically infers its type.
Syntax
name := value
Description
Most commonly used definition inside functions. No type is given, it is determined by the value.
Example
package main
import "fmt"
func main() {
name := "Alex"
age := 36
fmt.Println(name, age)
}Expected output
Alex 36
When to use?
Local variable definition.
Common mistakes
- ⚠️ := is only inside functions; use var at the package level.
- ⚠️ An unused variable is an ERROR in Go.