Go offers a full var declaration and a shorter := declaration. Both create variables, but they fit different locations and needs.
Go code
var score = 10
fmt.Println(score)Line-by-line explanation
varcreates score.- No type is written, so Go infers
intfrom 10. - Println displays the value.
Expected output
10The variable is still statically typed as an integer.
Go code
score := 10
fmt.Println(score)Line-by-line explanation
scoreis the new name.:=declares and assigns it.- Go infers the integer type.
- Println displays 10.
Expected output
10The result matches the var form.
Use := for clear local values inside a function. Use var when declaring outside a function, when you need an explicit type, or when the zero value is useful.