Using the var keyword
Variables declared without an explicit initial value are given their zero value.
Basic Declaration
You must specify eithertype or value (or both):
Learn more about Mintlify
Enter your email to receive updates about new features and product releases.
Learn how to declare variables in Go using var, :=, and const keywords with proper scoping rules
var keywordvar varName type = value
type or value (or both):
var name string = "John" // explicit type and value
var surname = "Doe" // type is inferred as string
var age int16 // explicit type, initialized to the zero value (0)
var name, age = "John", 30 // mixed types, types inferred
var a, b, c int = 1, 2, 3 // all must be of the declared type
var x, y int // both initialized to the zero value (0)
var (
name string = "John"
surname = "Doe" // type is inferred as string
age int // zero value (0)
)
:= syntaxvarName := value
name, age := "John", 30
const keywordconst varName type = value
const daysInWeek int = 7 // explicit type and value
const hoursInDay = 24 // type is inferred as int
const (
daysInWeek = 7
hoursInDay = 24
)