Declaring Variables
You cannot re-declare a variable that has already been declared in the same scope.
Using the var keyword
var varName type = valueVariables declared without an explicit initial value are given their zero value.
-
You must specify either
typeorvalue(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) -
Declaring multiple variables of the same or different types on a single line:
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) -
Using grouped declaration syntax to declare variables together:
var ( name string = "John" surname = "Doe" // type is inferred as string age int // zero value (0) )
Using the := syntax
varName := valueCan only be used inside functions, not at the package level.
-
Declaring multiple variables on a single line:
name, age := "John", 30
Using the const keyword
Constants are fixed values that cannot be changed after they are declared. They are read-only.
const varName type = value-
Constants can be declared without explicit types, but they must have values:
const daysInWeek int = 7 // explicit type and value const hoursInDay = 24 // type is inferred as int -
Declaring multiple constants in a single block:
const ( daysInWeek = 7 hoursInDay = 24 )
Computations on constants are mostly done at compile time, not at runtime.