In Go, variables act as containers for storing various types of data. By using variables, you can store values such as numbers, text, or logical states and use them throughout your program.
Go Variable Types
Go supports several variable types, such as:
int: For storing whole numbers, like42or-100.float32: For storing numbers with decimal points, such as3.14or-9.81.string: For storing textual data, like"Hello Go!"(enclosed in double quotes).bool: For storing logical states, which can be eithertrueorfalse.
The specific data type determines how the variable behaves and what kind of data it can store. For more details, refer to the Go Data Types lesson.
Declaring Variables
In Go, variables can be declared in two main ways:
Declaring with the var Keyword
The var keyword lets you explicitly define a variable’s name, type, and optionally its value.
Syntax:
var variableName type = value
You must specify either the type or the value (or both) when using var.
Declaring with the := Syntax
Go also supports shorthand variable declarations using :=. Here, the compiler determines the variable’s type based on its assigned value.
Syntax:
variableName := value
This shorthand requires assigning a value during declaration.
Variable Declaration with Initial Values
When the value of a variable is known at the time of declaration, it can be initialized in a single step.
Example:
package main
import ("fmt")
func main() {
var brand1 string = "Yamaha" // Explicitly specifying the type
var brand2 = "Honda" // Type is inferred
speed := 150 // Type is inferred as int
fmt.Println(brand1)
fmt.Println(brand2)
fmt.Println(speed)
}Here:
brand1is explicitly defined as astring.brand2andspeedhave their types inferred asstringandint, respectively.
Variable Declaration without Initial Values
In Go, if you declare a variable without initializing it, the compiler assigns it a default value based on its type.
Example:
package main
import ("fmt")
func main() {
var model string
var year int
var active bool
fmt.Println(model) // Default value: ""
fmt.Println(year) // Default value: 0
fmt.Println(active) // Default value: false
}In this example:
modeldefaults to an empty string"".yeardefaults to0.activedefaults tofalse.
Assigning Values After Declaration
You can assign a value to a variable after it has been declared.
Example:
package main
import ("fmt")
func main() {
var brand string
brand = "Suzuki"
fmt.Println(brand)
}Note: Variables declared using := must have a value during their declaration.
Differences Between var and :=
There are subtle differences between var and :=:
| Feature | var | := |
|---|---|---|
| Can be used inside and outside functions | Yes | No |
| Can separate declaration and assignment | Yes | No, must declare and assign value |
Example with var Outside Functions:
package main
import ("fmt")
var brand string = "Honda" // Declaring variable outside a function
func main() {
fmt.Println(brand)
}Example with := Outside Functions:
package main
import ("fmt")
brand := "Yamaha" // This will throw an error
func main() {
fmt.Println(brand)
}Result:
The code produces an error because := cannot be used outside a function:
./prog.go:5:1: syntax error: non-declaration statement outside function body


