Go variable
last modified April 11, 2024
In this article we show how to work with variables in Golang.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
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.
package main import "fmt" func main() { var i int = 1 var w float64 = 12.5 fmt.Println(i, w) }
In the code example, we declare and initialize two variables. Later, we print them.
$ go run declaring.go 1 12.5
Go declare multiple variables
With the var
keyword, we can declare multiple variables at once.
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
Go type inference
Go can infer the data type from the right side of the assignment.
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 code 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
Go shorthand variable declaration
Inside a function, the :=
short assignment statement can be used in
place of a var
declaration with implicit type.
package main import "fmt" func main() { name := "John Doe" age := 34 fmt.Printf("%s is %d years old\n", name, age) }
The example declares two variables with the shorhand notation.
Go shorthand multiple variable declaration
We can define multiple variables with the shorhand notation as well.
package main import "fmt" func main() { name, age := "John Doe", 34 fmt.Printf("%s is %d years old\n", name, age) }
The program declares two variables with the shorhand notation in one line.
Go variable default value
Variables declared without an explicit initial value are given their zero value:
- 0 - numeric types
- false - boolean type
- "" - string type
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 code 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.
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 code 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.
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 code example, we work with the age
variable and a WIDTH
constant.
Source
The Go Programming Language Specification
In this article we have covered variables in Golang.
Author
List all Go tutorials.