Learning Golang: Variables

Share on:

This is part 11 of my journey learning Golang.

Variables

A variable is a named value that can change during the execution of a program. The difference from a constant (which is also a named value) is that a variable’s value can be modified at run time.

Variable declarations

In Go, variables are declared following this pattern: var {identifier} {type}. Examples:

1var songName string
2var lengthOfSong uint16
3var isMusicOver bool
4var songRating float32

Zero values

If a value is not assigned to the variable when it is declared (like above) then it is initialized with a “zero value”. The actual value depends on the data type. Generally:

  • 0 for numeric data types
  • false for the boolean data type
  • empty string ("") for string data type

Default values

The initial value of a variable can be assigned when it is declared:

1var shoppingCartQuantity = 1
2var shoppingCartItemName = "cupholder"
3var shoppingCartPrice = 19.99
4var shoppingCartExtendedWarranty = true

Defining multiple variables

Go has a shorthand for declaring several variables in a single statement. Use the keyword var (or const) followed by parentheses with each variable on its own line:

1var (
2  a = 5
3  b = 10
4  c = 15
5)

Another way to declare multiple variables in a single statement, initialized with their zero value, is:

1var part1, part2 string

It’s also possible to declare multiple variables, while initializing them and letting their types be inferred from the value:

1anInteger, aString, aBoolean := 42, "Hello", true

Shorthand declaration

The := symbol is a shorthand for declaring a variable and assigning a default value. The type of the variable is inferred from the type of the value, instead of being explicitly declared.

Example:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  message := "Hello, world!"
 9  fmt.Println(message)
10}

Go offers another way to declare and initialize a variable inferring its type:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  var planet = "Earth"        // Inferred type: string
 9  var radiusInKm = 6371.009   // Inferred type: float64
10  var daysInTheYear = 365     // Inferred type: int (int64 or int32)
11  var hasMoon = true          // Inferred type: boolean
12  fmt.Printf("%T %T %T %T\n", planet, radiusInKm, daysInTheYear, hasMoon)
13}

The code above prints:

1string float64 int bool

Notice that values that infer their types from integer literals (like 365 above) will be of type int. On most computer systems, they will be 64 bits in length. On 32-bit systems, these variables will be 32 bits in length.

Updating a variable

The = symbol assigns a new value to a variable based on the expression at the right side of the statement (i.e. after “=”). = is called the “assignment operator”. It is the same operator that was used above to assign default values when a variable was declared. It can also be used at later parts of the code for updating the value of the variable (i.e. assigning a new value to it).

Example:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  var count uint64
 9  fmt.Println(count)
10  count = count + 1
11  fmt.Println(count)
12  count += 1
13  fmt.Println(count)
14  count = 99
15  fmt.Println(count)
16}

This will print:

10
21
32
499

The first value (0) is the “zero value” for numeric data types and it is the initial value of a numeric variable unless a different default value is set in the var statement.

The second value (1) is the result of the expression count + 1.

The third value (2) is the result of the expression count + 1. The += symbol is a shorthand for an arithmetic expression based on the variable’s current value.

The third value (99) is the result of the expression 99 (i.e. you can assign any value to the variable; you’re not limited to arithmetic expressions based on the previous value).


comments powered by Disqus