In this tutorial, you will learn what a variable is and how to declare, initialize and use different types of variables in the Go programming language.
A variable is nothing but a name provided to a storage location that can store data. It is the way to represent memory location using symbols so that it can be easily identified. The value of the variable can be changed and reuse many times.
The name of the variable should be unique across the program in order to easily identify.
Each variable is associated with a data type. Data types define the type of data and range of values that can be stored in the variable. Below are a few examples of the Go data type.
Data Type | Description |
int | Stores integer values like 5, 10, etc. |
float32 | Represents 32-bit floating-point numbers |
String | Stores string e.g. “tutorial” |
Boolean | Stores boolean values either true or false |
To know more about various data types, you can visit our tutorial Go Data Types.
Go variable syntax
Let’s check the syntax of the Go variable.
var variable_list optional_data_type;
Here,
- variable_list – variable_list can be made with one or more identifier names separated by commas.
- optional_data_type – Optional. This defines valid data types such as
int
,float32
,boolean
,complex64
, or any other user-defined object. This is optional because the type of the variable can be automatically decided by the Go compiler as per the value assigned in it.
Go variable example
Below are the some of valid variable declarations-
var a, b, c int; var i = 1; var mark, salary float32;
Go variable can be initialized during the declaration itself. The type of the variable is optional here, Go compiler can automatically decide the type of the variable based on the initialized value. For example-
var x int = 10; //or var x = 10;
In this example, var x = 10;
define a variable x
with data type int
(integer) and the initialized value is 10
.
package main import "fmt" func main() { var cnt int // variable declaration fmt.Println("Value of count is", cnt) }
If you do not initialize a variable the default value will be the zero value of their data type. Here, the variable cnt
is an integer type and the initial value for int is 0
.
Go single variable declaration
In the following example, we will check a simple Go program with the single variable declaration.
package main import "fmt" func main() { var count int // variable declaration fmt.Println("The value of count is =", count) }
Output
The statement var count int
declares a variable named count
and the type of the variable is int
. Also we have not assigned any value into it. Go compiler assign zero value of it’s type if any variable is declared but not initialized. You can see in the output that it has printed the value of the count as 0
. Go has assigned automatically the value of count = 0
as for int
type the zero value is 0
.
Once the variable is declared, the value can be assign to it anytime.
For example:
package main import "fmt" func main() { var count int // variable declaration count = 10 fmt.Println("The value of count is =", count) count = 11 fmt.Println("The new value of count is =", count) }
Output
The value of count is = 11
Go variable declaration with an initial value
A variable can also be declared and initialized at the same time.
Syntax
The below is the syntax for declaring a variable with an initial value.
var variable_name type = initial_value
Example
package main import "fmt" func main() { var count int =20 // variable declaration with initial value fmt.Println("The value of count is =", count) }
output
In the above program value of the variable count
has been initialized with the value 20
.
Type inference
It is not mandatory to declare the type of the variable in Go programming always. If the variable has been assigned an initial value, Go automatically infer the type of the variable using the initial value.
Syntax
var variable_name = initial_value
Example
package main import "fmt" func main() { var count = 10 // variable declaration with initial value fmt.Println("The value of count is =", count) }
You can notice from the above example that the type of the variable count hasn’t mentioned. But it has been initialized with 10
. Go will automatically infer its type as int
.
Output
Short hand declaration
Another popular way to declare the Go variable is using short hand variable. In this case, :=
is used to declare and initialized a variable.
Syntax
variable_name := initial_value
Example
Below is an example of a short hand declaration.
package main import "fmt" func main() { count := 20 // variable declaration using short hand declaration fmt.Println("The value of count is =", count) }
In the above example, the variable count
is declared and initialized using short hand declaration. It has been initialized with the value 20
. Go will automatically infer the type of the count
as int as it has been initialized with the integer value 20
.
Output
Multiple variable declaration
It is also possible to declare multiple variables in a single statement.
Syntax
var variable_name1, variable_name2 type = initialvalue1, initialvalue2
Example
package main import "fmt" func main() { var min, max int = 5, 100 //declaring multiple variables fmt.Println("Minimum value is", min, "Maximum value is", max) }
In the above example, the statement var min, max int = 5, 100
declares two variables min
and max
of int
type and initialized with the value 5
and 100
respectively.
Output
The type of the variables can be removed if those are declared with the initial values. In the above example, we have initial value for variables so type can be removed.
package main import "fmt" func main() { var min, max = 5, 100 //declaring multiple variables fmt.Println("Minimum value is", min, "Maximum value is", max) }
There might be the case when we need to declare multiple variables of different types in a single statement. In this case the syntax will be as below.
var ( variable_name1 = initialvalue1 variable_name2 = initialvalue2 )
The following example shows the declaration of multiple variables of different types.
package main import "fmt" func main() { var ( name = "Sagar" age = 30 height int ) fmt.Println("His name is", name, ", age is", age, "and height is", height) }
Output