Modern programming languages are built with primitives called types. When you hear that a variable is a string or integer, you are talking about the variable's type.
With today's programming languages, there are two common type systems used:
- Dynamic types (also called duck typing)
- Static types
Go is a statically typed language. For many of you who might be coming from languages such as Python, Perl, and PHP, then those languages are dynamically typed.
In a dynamically typed language, you can create a variable and store anything in it. In those languages, the type simply indicates what is stored in the variable. Here is an example in Python:
v = "hello"
v = 8
v = 2.5
In this case, v
can store anything, and the type held by v
is unknown without using some runtime checks (runtime meaning that it can't be checked at compile time).
In a statically typed language, the type of the variable is set when it is created. That type cannot change. In this type of language, the type is both what is stored in the variable and what can be stored in the variable. Here is a Go example:
v := "hello" // also can do: var v string = "hello"
The v
value cannot be set to any other type than a string.
It might seem like Python is superior because it can store anything in its variable. But in practice, this lack of being specific means that Python must wait until a program is running before it can find out there is a problem (what we call a runtime error). It is better to find the problem when the software is compiled than when it is deployed.
Let's take a look at a function to add two numbers together as an example.
Here is the Python version:
def add(a, b):
return a+b
Here is the Go version:
func add(a int, b int) int {
return a + b
}
In the Python version, we can see that a
and b
will be added together. But, what types are a
and b
? What is the result type? What happens if I pass an integer and a float or an integer and a string?
In some cases, two types cannot be added together in Python, which will cause a runtime exception, and you can never be sure of what the result type will be.
Note
Python has added type hints to the language to help avoid these problems. But, practical experience has taught us with JavaScript/Dart/TypeScript/Closure that while it can help, optional type support means that a lot of problems fall through the cracks.
Our Go version defines the exact types for our arguments and our result. You cannot pass an integer and a float or an integer and a string. You will only ever receive an integer as a return. This allows our compiler to find any errors with variable types when the program is compiled. In Python, this error could show up at any time, from the instant it ran to 6 months later when a certain code path was executed.
Note
A few years ago, there was a study done on the Rosetta Code repository for some of the top languages in use to see how they fared in processing time, memory use, and runtime failures. For runtime failures, Go had the least failures, with Python towards the bottom of the ranking. Static typing would have certainly played into that.
The study can be found here: https://arxiv.org/pdf/1409.0252.pdf.
Go's types
Go has a rich type system that not only specifies that a type might be an integer but also the size of the integer. This allows a Go programmer to reduce the size of a variable both in memory and when encoding for network transport.
The following table shows the most common types used in Go:
Table 1.1 – Common types used in Go and their descriptions
We will be keeping our discussion mostly to the preceding types; however, the following table is the full list of types that can be used:
Table 1.2 – Full list of types that you can use in Go
Go doesn't just provide these types; you can also create new types based on these basic types. These custom types become their own type and can have methods attached to them.
Declaring a custom type is done with the type
keyword and will be discussed during the section on the struct
type. For now, we are going to move on to the basics of declaring variables.
Now that we've talked about our variable types, let's have a look at how we can create them.
Declaring variables
As in most languages, declaring a variable allocates storage that will hold some type of data. In Go, that data is typed so that only that type can be stored in the allocated storage. As Go has multiple ways to declare a variable, the next parts will talk about the different ways this can be done.
The long way to declare a variable
The most specific way to declare a variable is using the var
keyword. You can use var
to declare a variable both at the package level (meaning not inside a function) and within a function. Let's look at some examples of ways to declare variables using var
:
var i int64
This declares an i
variable that can hold an int64
type. No value is assigned, so the value is assigned the zero value of an integer, which is 0
:
var i int = 3
This declares an i
variable that can hold an int
type. The value 3
is assigned to i
.
Note that the int
and int64
types are distinct. You cannot use an int
type as an int64
type, and vice versa. However, you can do type conversions to allow interchanging these types. This is discussed later:
var (
i int
word = "hello"
)
Using ()
, we group together a set of declarations. i
can hold an int
type and has the integer zero value, 0
. word
doesn't declare the type, but it is inferred by the string value on the right side of the equal (=
) operator.
The shorter way
In the previous example, we used the var
keyword to create a variable and the =
operator to assign values. If we do not have an =
operator, the compiler assigns the zero value for the type (more on this later).
The important concept is as follows:
var
created the variable but did not make an assignment.
=
assigned a value to the variable.
Within a function (not at the package level), we can do a create and assign by using the :=
operator. This both creates a new variable and assigns a value to it:
i := 1 // i is the int type
word := "hello" // word is the string type
f := 3.2 // f is the float64 type
The important thing to remember when using :=
is that it means create and assign. If the variable already exists, you cannot use :=
, but must use =
, which just does an assignment.
Variable scopes and shadowing
A scope is the part of the program in which a variable can be seen. In Go, we have the following variable scopes:
- Package scoped: Can be seen by the entire package and is declared outside a function
- Function scoped: Can be seen within
{}
which defines the function
- Statement scoped: Can be seen within
{}
of a statement in a function (for
loop, if
/else
)
In the following program, the word
variable is declared at the package level. It can be used by any function defined in the package:
package main
import "fmt"
var word = "hello"
func main() {
fmt.Println(word)
}
In the following program, the word
variable is defined inside the main()
function and can only be used inside {}
which defines main
. Outside, it is undefined:
package main
import "fmt"
func main() {
var word string = "hello"
fmt.Println(word)
}
Finally, in this program, i
is statement scoped. It can be used on the line starting our for
loop and inside {}
of the loop, but it doesn't exist outside the loop:
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
The best way to think of this is that if your variable is declared on a line that has {
or within a set of {}
, it can only be seen within those {}
.
Cannot redeclare a variable in the same scope
The rule for this, You cannot declare two variables with the same name within the same scope.
This means that no two variables within the same scope can have the same name:
func main() {
var word = "hello"
var word = "world"
fmt.Println(word)
}
This program is invalid and will generate a compile error. Once you have declared the word
variable, you cannot recreate it within the same scope. You can change the value to a new value, but you cannot create a second variable with the same name.
To assign word
a new value, simply remove var
from the line. var
says create variable where we want to only do an assignment:
func main() {
var word = "hello"
word = "world"
fmt.Println(word)
}
Next, we will look at what happens when you declare two variables with the same name in the same scope, but within separate code blocks.
Variable shadowing
Variable shadowing occurs when a variable that is within your variable scope, but not in your local scope, is redeclared. This causes the local scope to lose access to the outer scoped variable:
package main
import "fmt"
var word = "hello"
func main() {
var word = "world"
fmt.Println("inside main(): ", word)
printOutter()
}
func printOutter() {
fmt.Println("the package level 'word' var: ", word)
}
As you can see, word
is declared at the package level. But inside main
, we define a new word
variable, which overshadows the package level variable. When we refer to word
now, we are using the one defined inside main()
.
printOutter()
is called, but it doesn't have a locally shadowed word
variable (one declared between its {}
), so it used the one at the package level.
Here's the output of this program:
inside main(): world
the package level 'word' var: hello
This is one of the more common bugs for Go developers.
Zero values
In some older languages, a variable declaration without an assignment has an unknown value. This is because the program creates a place in memory to store the value but doesn't put anything in it. So, the bits representing the value are set to whatever happened to be in that memory space before you created the variable.
This has led to many unfortunate bugs. So, in Go, declaring a variable without an assignment automatically assigns a value called the zero value. Here is a list of the zero values for Go types:
Table 1.3 – Zero values for Go types
Now that we understand what zero values are, let's see how Go prevents unused variables in our code.
Function/statement variable must be used
The rule here is that if you create a variable within a function or statement, it must be used. This is much for the same reason as package imports; declaring a variable that isn't used is almost always a mistake.
This can be relaxed in much the same way as an import, using _
, but is far less common. This assigns the value stored in someVar
to nothing:
_ = someVar
This assigns the value returned by someFunc()
to nothing:
_ = someFunc()
The most common use for this is when a function returns multiple values, but you only need one:
needed, _ := someFunc()
Here, we create and assign to the needed
variable, but the second value isn't something we use, so we drop it.
This section has provided the knowledge of Go's basic types, the different ways to declare a variable, the rules around variable scopes and shadows, and Go's zero values.