Let’s take a look at variables and data types available in Go programming language. We will cover primitive data types and complex data types briefly and see different options for variable declaration.
Variables are identifiers which reference memory address where the actual value assigned to the variable is stored. When we use this variable, the compiler uses this named memory address to retrieve the value stored at that address.
Variable Declarations
There are several ways to declare and initialize variables in Go. Go has several built-in types. The general syntax for declaring a variable looks like below.
1var <variable_name> <data_type>
Whenever you create a variable, you must include its type, either explicitly or by assigning a value from which the compiler will identify the type of the variable. Also variables are also identifiers so they follow naming rules. They must start with a letter or underscore followed by letters, numbers or underscores.
Above syntax only declares the variables and do not assign any specific values. For example, in below code we declare variables of type int, string, float32 and bool. We will learn about this data types later in this lesson.
1var id int
2var name string
3var price float32
4var isActive bool
When we declare the variable without initializing, the variables get what is called zero value in Go. These are like default values in other programming languages. So, for example, in above code, the value of id will be 0 even though we have not explicitly assigned the value 0 to it. The value of variable name will be empty string and similarly price will have 0.00 and isActive will be false.
Example:
1package main
2
3import "fmt"
4
5func main() {
6 var id int
7 var name string
8 var price float32
9 var isActive bool
10
11 fmt.Println(id)
12 fmt.Println(name)
13 fmt.Println(price)
14 fmt.Println(isActive)
15}
Output:
go run main.go
0
0
false
We can also declare multiple variable in a single line using var name, email string format. In this statement, both name and email are declared to be of type string in the same line.
Variable Initialization
We can also provide specific value to a variable when we declare a variable. This is called initialization or assignment. We can do that like below where we use assignment operator (=) to assign value on the right side to the variable.
1var id int = 5
2var name string = "Jack"
We can also declare multiple variables in single statement as long as their data types are same. For example, in below code, on line 11, we declare and initialize two variables a and b with values 20 and 30. We can also print two variables together by separating those variables with comma (,) in fmt.Println function as shown in line 18.
Example:
1package main
2
3import "fmt"
4
5func main() {
6 var id int = 5 // declare and initialize int variable
7 var name string = "Jack" // string variable
8 var price float32 = 99.99 // float
9 var isActive bool = true // boolean
10 var largeInt int64 = 23434523 // int64 data type
11 var a, b int = 20, 30 // declare and initialize multiple variables
12
13 fmt.Println(id)
14 fmt.Println(name)
15 fmt.Println(price)
16 fmt.Println(isActive)
17 fmt.Println(largeInt)
18 fmt.Println(a, b) // print multiple variables
19}
go run main.go
Output:
5
Jack
99.99
true
23434523
20 30
Shorthand assignment
So far, the kind of variable assignments above were of static type. That is we specify the type of the variable to the compiler before assigning the value. Go supports dynamic style typing as well where we do not specify the type of the variable like Python or Ruby. However, Go is still statically typed language, so compiler does that extra work for you to infer the data type of the variable. For this we use shorthand assignment.
Shorthand notation for declaring and initializing values to the variable uses := operator. This is used only for initialization of the variable and once initialized, we have to use assignment operator (=) to modify variable’s value. This is generally the preferred method for declaring and initializing variables in Go community.
1a := 10
2b, c := 20, 30
3a = 100
In this case, we have not specified the data type of the variable a, b and c. However, based on the values being assigned on the right side, the compiler infers the data types of those variables as int.
Example:
1package main
2
3import "fmt"
4
5func main() {
6 a := 10
7 b, c := 20, 30
8 fmt.Println(a, b, c)
9 a = 100
10 fmt.Println(a)
11}
Output:
go run main.go
10 20 30
100
If we try to re-assign these variables with string data type, the compiler throws an error. This is the first encouter with error so try to read the error and understand. Here, it says “Hello” which is string cannot be used as int in assignment because of static type checking.
1package main
2
3import "fmt"
4
5func main() {
6 a := 10
7 fmt.Println(a)
8 a = "Hello" // This line fails
9}
go run main.go
# command-line-arguments
./main.go:8:9: cannot use "Hello" (untyped string constant) as int value in assignment
Variable Scopes
Variable scope defines what part of the program can access the variables defined. Go has two types of variable scopes
- Local scope: These are accessible within the block of code or function where the variable is defined.
- Global scope: These are usually declared at the beginning of the source file, outside of any block or function. These are accessible anywhere in the program.
It’s always advisable to use local variables rather than global unless there is specific reason you need the value across the whole source file.
Constants
Constants are like variables however once initialized, the value cannot be modified. These are declared using const keyword. We cannot declare a constant without initializing a value, i.e. the value cannot be assigned later or zero value does not apply to constants. Also, shorthand variable assignment does not apply to constants. They will usually have a global scope. The basic syntax for constants look like this.
1const <constant_name> <datatype> = <value>
For example,
1const PI float32 = 3.14159
Data Types
As we saw in examples above that we have data types for primitive types like integer, boolean, float and we also have string data types. We also have some collection data types which help store group of data. This is how most of the programs use computer memory. These collection data types are created with these primitive types. Below table shows the data type and their range.
| Data Type | Description |
|---|---|
| int8 | 8-bit signed integer (-128 to 127) |
| int16 | 16-bit signed integer (-32768 to 32767) |
| int/ int32 | 32-bit signed integer |
| int64 | 64-bit signed integer |
| uint8 | 8-bit unsigned integer (0 to 255) |
| byte | 8-bit unsigned integer, synonymous to uint8 |
| uint16 | 16-bit unsigned integer |
| uint/uint32 | 32-bit unsigned integer |
| uint64 | 64-bit unsigned integer |
| uintptr | pointer of type unsigned integer |
| float32 | 32-bit floating pointer number |
| float64 | 64-bit floating point number |
| complex64 | complex number with 32-bit real and imaginary parts |
| complex128 | complex number with 64-bit real and imaginary parts |
| bool | boolean |
| rune | same as int32 |
Go support int, uint and uintptr which are implementation-specific types. Their allowed min and max range depends on the computer architecture. If the code is running on 32-bit architecture then int type will be of 32-bit integer. If the code is running on 64-bit architecture, the range will be 64-bit integer range.
There are also other container data types which we will discuss in lot more detail in this tutorial series. These are arrays, slices, maps and structs. Below table gives brief idea about those, but we will see their examples throughout the tutorials.
| Data Type | Description |
|---|---|
| array | These are fixed length container variable which can hold data of the same type |
| slice | These are similar to Arrays but can grow as we add more data values. |
| map | These are key-value pairs. These are called map, dictionary or associative arrays in other languages |
| struct | These are like structure in C and helps like classes in other object oriented programming languages |
Define Custom Data types
We can also define our own types using type keyword. This is usually used with another data types called structs.
1type UserId int
2type Username string
Here, UserId is user-defined type which will have value of type int. However, this makes lot more readable code when used for User collection to store user specific information.


Comments