Variables

This is the third tutorial in our Golang tutorial series and it deals with variables in Golang.

You can read part 2 to learn about configuring Go and running the hello world program.

What is a variable?

Variable is the name given to a memory location to store a value of a specific type. There are various syntaxes to declare variables in Go. Let’s look at them one by one.

Declaring a single variable

var name type is the syntax to declare a single variable.

1package main
2
3import "fmt"
4
5func main() {
6	var age int // variable declaration
7	fmt.Println("My initial age is", age)
8}

Run in playground

The statement var age int declares a variable named age of type int. We have not assigned any value to this variable. If a variable is not assigned any value, Go automatically initializes it with the zero value of the variable’s type. In this case, age is assigned the value 0, which is the zero value of int. If you run this program, you can see the following output.

My initial age is 0  

A variable can be assigned to any value of its type. In the above program, age can be assigned any integer value.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	var age int // variable declaration
 7	fmt.Println("My initial age is", age)
 8	age = 29 //assignment
 9	fmt.Println("My age after first assignment is", age)
10	age = 54 //assignment
11	fmt.Println("My age after second assignment is", age)
12}

Run in playground

The above program will print the following output.

My initial age is 0
My age after first assignment is 29
My age after second assignment is 54

Declaring a variable with an initial value

A variable can also be initialized with a value when it is declared. The following is the syntax to declare a variable with an initial value.

var name type = initialvalue
1package main
2
3import "fmt"
4
5func main() {
6	var age int = 29 // variable declaration with initial value
7
8	fmt.Println("My initial age is", age)
9}

Run in playground

In the above program, age is a variable of type int and has initial value 29. The above program will print the following output.

My initial age is 29 

It confirms that age has been initialized with the value 29.

Type inference

If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value. Hence if a variable has an initial value, the type in the variable declaration can be removed.

If the variable is declared using the following syntax

var name = initialvalue

Go will automatically infer the type of that variable from the initial value.

In the following example, we can see that the type int of the variable age has been removed in line no. 6. Since the variable has an initial value 29, Go can infer that it is of type int.

1package main
2
3import "fmt"
4
5func main() {
6	var age = 29 // type will be inferred
7	fmt.Println("My initial age is", age)
8}

Run in playground

Multiple variable declaration

Multiple variables can be declared using a single statement.

var name1, name2 type = initialvalue1, initialvalue2 is the syntax for multiple variable declaration.

1package main
2
3import "fmt"
4
5func main() {
6	var price, quantity int = 5000, 100 //declaring multiple variables
7
8	fmt.Println("price is", price, "quantity is", quantity)
9}

Run in playground

The type can be removed if the variables have an initial value. Since the above program has initial values for variables, the int type can be removed.

1package main
2
3import "fmt"
4
5func main() {
6	var price, quantity = 5000, 100 //declaring multiple variables with type inference
7
8	fmt.Println("price is", price, "quantity is", quantity)
9}

Run in playground

The above program will print

price is 5000 quantity is 100

As you would have probably guessed by now, if the initial value is not specified for price and quantity, they will have 0 assigned as their initial value.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	var price, quantity int
 7	fmt.Println("price is", price, "quantity is", quantity)
 8	price = 3000
 9	quantity = 500
10	fmt.Println("new price is", price, "new quantity is", quantity)
11}

Run in playground

The above program will print

price is 0 quantity is 0
new price is 3000 new quantity is 500

There might be cases where we would want to declare variables belonging to different types in a single statement. The syntax for doing that is

1var (
2      name1 = initialvalue1
3      name2 = initialvalue2
4)

The following program uses the above syntax to declare variables of different types.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	var (
 7		name   = "Naveen"
 8		age    = 38
 9		height int
10	)
11	fmt.Println("my name is", name)
12	fmt.Println("my age is", age)
13	fmt.Println("my height is", height)
14}

Run in playground

Here we declare a variable name of type string, age and height of type int. (We will discuss the various types available in Golang in the next tutorial).

Running the above program will print

my name is Naveen
my age is 38
my height is 0

Short hand declaration

Go also provides another concise way to declare variables. This is known as short hand declaration and it uses := operator.

name := initialvalue is the short hand syntax to declare a variable.

The following program uses the short hand syntax to declare a variable count initialized to 10. Go will automatically infer that count is of type int since it has been initialized with the integer value 10.

1package main
2
3import "fmt"
4
5func main() {
6	count := 10
7	fmt.Println("Count =",count)
8}

Run in playground

The above program will print,

Count = 10

It is also possible to declare multiple variables in a single line using short hand syntax.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	name, age := "Naveen", 29 //short hand declaration
 7
 8	fmt.Println("my name is", name)
 9	fmt.Println("my age is", age)
10}

Run in playground

The above program declares two variables name and age of type string and int respectively.

If you run the above program, you can see

my name is Naveen
my age is 29

getting printed.

Short hand declaration requires initial values for all variables on the left-hand side of the assignment. The following program will print an error assignment mismatch: 2 variables but 1 value. This is because age has not been assigned a value.

1package main
2
3import "fmt"
4
5func main() {
6	name, age := "Naveen" //error
7
8	fmt.Println("my name is", name, "age is", age)
9}

Run in playground

Short hand syntax can only be used when at least one of the variables on the left side of := is newly declared. Consider the following program,

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	a, b := 20, 30 // declare variables a and b
 7	fmt.Println("a is", a, "b is", b)
 8	b, c := 40, 50 // b is already declared but c is new
 9	fmt.Println("b is", b, "c is", c)
10	b, c = 80, 90 // assign new values to already declared variables b and c
11	fmt.Println("changed b is", b, "c is", c)
12}

Run in playground

In the above program, in line no. 8, b has already been declared but c is newly declared and hence it works and outputs

a is 20 b is 30
b is 40 c is 50
changed b is 80 c is 90

Whereas if we run the program below,

1package main
2
3import "fmt"
4
5func main() {
6	a, b := 20, 30 //a and b declared
7	fmt.Println("a is", a, "b is", b)
8	a, b := 40, 50 //error, no new variables
9}

Run in playground

it will print error ./prog.go:8:7: no new variables on left side of := This is because both the variables a and b have already been declared and there are no new variables in the left side of := in line no. 8

Variables can also be assigned values which are computed during run time. Consider the following program,

 1package main
 2
 3import (
 4	"fmt"
 5	"math"
 6)
 7
 8func main() {
 9	a, b := 145.8, 543.8
10	c := math.Min(a, b)
11	fmt.Println("Minimum value is", c)
12}

Run in playground

In the above program math is a package and Min is a function in that package. Don’t worry about it right now, we will discuss packages and functions in detail in the upcoming tutorials. All we need to know is, the value of c is calculated at run time and it’s the minimum of a and b. The program above will print,

Minimum value is  145.8

Since Go is strongly typed, variables declared as belonging to one type cannot be assigned a value of another type. The following program will print an error cannot use "Naveen" (untyped string constant) as int value in assignment because age is declared as type int and we are trying to assign a string value to it.

1package main
2
3func main() {
4	age := 29      // age is int
5	age = "Naveen" // error since we are trying to assign a string to a variable of type int
6}

Run in playground

Thanks for reading. Please post your feedback and queries in the comments section. Please consider sharing this tutorial on twitter or LinkedIn. Have a good day.

Next tutorial - Types