Signup/Sign In
PUBLISHED ON: JUNE 1, 2021

Variables in Go

A Variable is an identifier or name that is used to hold a value of any type during runtime. Variables are used to store some values that can later be changed or accessed. In Go, we can use the var keyword for declaring a variable.

Syntax for Go Variable

The syntax for variable declaration looks like this:

var identifier type

We define variable in Go language using the var keyword.

The identifier is the name of the variable, it is followed by the type which is basically the datatype of the variable we are declaring. It should be noted that the type of the variable comes after the declaration of the name of the variable which is contrary to most of the older programming languages.

Variable Declaration

Let's see how to declare a variable in Go, create some variables with different data types and see how to access them. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var myName string
	fmt.Println(myName)

	var collegeId int
	fmt.Println(collegeId)
}

In the above example, we created two variables, named myName and collegeId and both of them are of different data types. One is a string and the other is an int. Just after the variable declaration, we printed the variables by passing the name of the variables inside the Println() function.



0

Notice the output seems a bit strange at first, as for the first Println() function we get nothing printed out to the terminal, and for the second Println() function we get 0 printed to the terminal. It is the default value of the int type.

Default Variable Values in Go

Whenever we declare a variable in Go and don't initialize it with any value, Go runtime automatically assigns the default value of that particular data type to it.

For example, if we declare a variable of type int, the default value will be 0, in fact, the variable is of integer type then the default value remains 0. Also, if the variable is of type string then the default value is an empty string i.e "".

The default values for different data types are:

Type Default Value
int or integer values 0
bool false
string empty string
pointer nil
struct {}

Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var collegeId int
	fmt.Println(collegeId)

	var someTruth bool
	fmt.Println(someTruth)

	var someFloatValue float64
	fmt.Println(someFloatValue)
}


0
false
0

Assigning a value to a variable

Assigning a value to the variable means setting a value to it. The value is assigned to a variable using the assignment operator( = ). It is possible to access and even modify the value at runtime. The syntax of value assignment looks something like this:

var identifier type = value

Consider the example shown below for variable assignment.

package main

import (
	"fmt"
)

func main() {
	var collegeId int = 10602
	fmt.Println(collegeId)

	var someTruth bool = true
	fmt.Println(someTruth)

	var someFloatValue float64 = 32.71
	fmt.Println(someFloatValue)
}

In the above example, we declared three different variables of different data types and we assigned values to each of these variables using the = operator. Later, we can access these values at runtime inside the Println() function.


10602
true
32.71

In all the examples above, we declared variables inside the scope of the main function only. Go also allows us to declare variables outside the main function, and we can do that consider the example shown below:

package main

import (
	"fmt"
)

var myName string = "Mukul"
var collegeId int = 10602

func main() {
	fmt.Println(myName)
	fmt.Println(collegeId)
}


Mukul
10602

Combine Variable Declaration

It is also possible to group variables together and we don't necessarily need to write the var keyword again and again. Consider the example shown below:

package main

import (
	"fmt"
)

var (
	myName    string = "Mukul"
	collegeId int    = 10602
)

func main() {
	fmt.Println(myName)
	fmt.Println(collegeId)
}


Mukul
10602

What is Automatic Type Inference in Go?

Go provides a simpler syntax in terms of variable declaration. We can even remove the type of the variable when we are declaring it and the compiler will automatically infer the type of the variable based on the assigned value. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var name = "Mukul"
	var collegeID = 1416
	fmt.Println("Name is", name, "ID is", collegeID)
}

In the above example, we declared two variables, and in both the declarations of variables we didn't mention the type of the variables. The compiler implicitly got the type of the variable from the right side of the assignment and hence it worked.


Name is Mukul ID is 1416

Can we do Multiple variables declaration in Go?

Yes, Go language also allows us to declare multiple variables at once. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var name, collegeID = "Mukul", 1416
	fmt.Println("Name is", name, "ID is", collegeID)
}


Name is Mukul ID is 1416

Short assignment operator

In Go, we have a short assignment operator which is the shortest way to declare a variable. The syntax involves a := symbol and we don't need the var keyword. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	name := "Mukul"
	collegeID := 1416
	fmt.Println("Name is",name,"ID is",collegeID)
}


Name is Mukul ID is 1416

It should be noted that we can't reassign a variable with the := operator as using it means we are initializing and assigning at the same time. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	var name string = "Rahul"
	name := "Mukul"
	collegeID := 1416
	fmt.Println("Name is", name, "ID is", collegeID)
}

The above code will result in an error.


./prog.go:9:7: no new variables on the left side of :=

We cannot use the short assignment operator outside the scope of the main function or any function, as outside any function, every construct begins with a keyword(var, const, func and so on). Consider the example shown below:

package main

import (
	"fmt"
)

name := "Mukul"

func main() {
	fmt.Println("Name is", name)
}

The above code will result in an error.


./prog.go:7:1: syntax error: non-declaration statement outside function body

Conclusion

In the above article, we learned what variables are, how can we declare variables in Go. We also learned what zero values of different types of variables are there in Go, then later we assigned some values to the variables. We also learned how to make use of the fact that the compiler can infer the type, then we learned about multiple variable declarations, and lastly, we learned about the short assignment operator.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.