Variables in Go
Variables are used to store values. They are labels given to the values. Go uses the var
keyword to declare a list of variables. We can also use the :=
shorthand syntax to declare variables.
Variables can hold values of different data types. A data type
is a set of values and the allowable operations on those values. In many cases, Go can infer the data type from the right side of the assignment.
The values that variables store can change over time. Constants, on the other hand, do not change their values once defined.
Go declare variables
The type of the variable follows the variable name.declaring.go
package main import "fmt" func main() { var i int = 1 var w float64 = 12.5 fmt.Println(i, w) }
In the example, we declare and initialize two variables. Later, we print them.
$ go run declaring.go 1 12.5
This is the output.
Go declare multiple variables
With the var
keyword, we can declare multiple variables at once.multiple.go
package main import ( "fmt" ) func main() { var i, j, k = 1, 2, 3 var ( name = "John Doe" occupation = "gardener" ) fmt.Println(i, j, k) fmt.Printf("%s is a %s\n", name, occupation) }
The example shows how to declare multiple variables with var
.
$ go run var_init.go 1 2 3 John Doe is a gardener
This is the output.
Go type inference
Go can infer the data type from the right side of the assignment.inference.go
package main import ( "fmt" "reflect" ) func main() { var name = "John Doe" var age = 34 fmt.Println(reflect.TypeOf(name)) fmt.Println(reflect.TypeOf(age)) fmt.Printf("%s is %d years old\n", name, age) }
In the example, we define two variables without specifying their data type. The data types are inferred.
var name = "John Doe" var age = 34
In order for the inference to work, the variables must be initialized.
fmt.Println(reflect.TypeOf(name)) fmt.Println(reflect.TypeOf(age))
With the help of the TypeOf
function from the reflect
package, we print the data types of the two variables.
$ go run inference.go string int John Doe is 34 years old
This is the output.
Go shorthand variable declaration
Inside a function, the :=
short assignment statement can be used in place of a var
declaration with implicit type.shorthand.go
package main import "fmt" func main() { name := "John Doe" age := 34 fmt.Println("%s is %d years old", name, age) }
The example declares two variables with the shorhand notation.
Go variable default value
Variables declared without an explicit initial value are given their zero value:
- 0 – numeric types
- false – boolean type
- “” – string type
default.go
package main import "fmt" func main() { var age int var isPresent bool var name string var weight float64 fmt.Println(age, isPresent, name, weight) }
The four variables in the example are given their default values.
Go variable scope
The scope of a variable is a region of code where the variable can be referenced.scope.go
package main import "fmt" var word string = "falcon" func main() { i := 12 fmt.Println(word) fmt.Println(i) test() } func test() { fmt.Println(word) }
In the example, we have two variables defined.
var word string = "falcon"
The word
variable is defined in the global scope. It is visible in both main
and test
functions.
func main() { i := 12 ...
The i
variable has a local scope. It is visible only inside the main
function.
Go constant
Unlike variables, constants cannot change their values over time. They are defined with the const
keyword. Constants are written in uppercase letters by convention.constant.go
package main import "fmt" func main() { var age int = 34 const WIDTH = 100 age = 35 age = 36 // WIDTH = 101 fmt.Println(age, WIDTH) }
In the example, we work with the age
variable and a WIDTH
constant.