Go Variables

Summary: in this tutorial, you will learn about Go variables, and how to declare and use variables.

In programming, you often need to store and manipulate data. To do that, you use variables.

Variables are identifiers whose values can be changed throughout the program.

Declaring variables

To declare a variable, you use the var keyword, followed by the variable name, and the data type:

var variableName dataType;Code language: Go (go)

For example, the following declares a variable named qty with the type integer:

var qty intCode language: Go (go)

In this example:

  • var is the keyword that instructs Go to declare a variable.
  • qty is the name of the variable that will hold an integer.
  • int is the data type of integer.

Go has many data types but the basic ones that you need to know for now are:

  • int: integer type
  • float64: 64-bit floating-point number.
  • bool: the boolean type that has two values true and false.
  • string: a sequence of UTF-8 encoded characters.

When declaring a variable without an initial value, it is automatically assigned the zero value of its type. For example 0 for int, "" for string, and false for bool, etc.

Go does not have uninitialized variables, which makes the program more robust and avoids many potential errors.

Unlike other languages, Go doesn’t allow unused variables. If you declare a variable and do not use it, Go will issue a compilation error. This promotes cleaner code.

If you want to declare and assign an initial value to a variable, you can do it using a single statement:

var variableName type = initialValueCode language: Go (go)

For example, the following declares a variable qty and initialize its value to 100:

var qty int = 10Code language: Go (go)

Type inferences

Go can infer the variable type if you declare a variable with an initial value. For example:

var qty int = 10Code language: Go (go)

In this example, we assign the number 10 to the qty variable. Go can know that the type of the qty is int. Therefore, Go allows you to omit the type after the variable name like this:

var qty = 10Code language: Go (go)

It’s more concise.

Short variable declaration

The most concise way to declare and initialize a variable is to use the shorthand:

qty := 10Code language: Go (go)

In this example, instead of using the = operator, we use the := operator.

The := operator allows you to declare and initialize a variable in one step without specifying the var keyword and the variable type. Go will infer the type of the variable automatically from the value on the right-hand side of the assignment.

Note that you can declare and initialize variables using the := operator inside a function.

Declaring multiple variables

Go allows you to declare multiple variables of the same type in a single statement. For example:

var qty, index intCode language: Go (go)

And initialize them with different values:

var qty, index = 100, 0Code language: Go (go)

The values of qty and index variables are 100 and 0 respectively.

Alternatively, you can use the short variable declaration:

qty, index := 100, 0Code language: Go (go)

If you have multiple variables but different types, you need to declare them separately, like this:

var int qty
var float64 amountCode language: Go (go)

Alternatively, you can use a short variable declaration:

qty, amount := 100, 9.99Code language: Go (go)

Assigning values to variables

To assign a value to a variable, you use the assignment operator (=):

variableName = newValueCode language: Go (go)

For example:

var qty int
qty = 200Code language: Go (go)

Casting values

Go allows you to convert a value of a type to another. To cast a value to a type, you use the following syntax:

v1 = type(v2)

For example, the following program shows how to cast a float to an integer:

package main

import "fmt"

func main() {
    var qty int = 10
    var amount float64 = 9.5

    qty = int(amount)
    fmt.Println(qty)
}
Code language: JavaScript (javascript)

Output:

9

Go variable examples

The following program shows some examples of using variables:

package main

import "fmt"

func main() {
    qty, amount := 10, 9.95
    total := float64(qty) * amount
    fmt.Printf("Total: $%0.2f", total)
}Code language: Go (go)

Output:

Total: $99.50Code language: Go (go)

How it works.

First, declare and initialize the qty and amount variable with the int and float64 types and values 10 and 9.95 respectively:

qty, amount := 10, 9.95Code language: Go (go)

Second, declare the total variable and assign a value that comes from the multiplication of the qty and amount variables:

total := float64(qty) * amountCode language: Go (go)

Since the qty variable has a type of integer, which is not compatible with the float64 type, we need to convert it to float64 using float64() before multiplying.

Third, print the result formatted as monetary value with two decimal places using the Printf function of the fmt package:

fmt.Printf("Total: $%0.2f", total)Code language: Go (go)

The "%0.2f" formats the total value as a floating-point number with two decimal places.

Summary

  • Use the var variableName type syntax to declare a variable
  • Use the var variableName type = initialValue syntax to declare and initialize a variable.
  • Use the shorthand syntax variableName := initialValue to both declare and initialize a variable to make your code more concise.
  • Go uses type inference to guess the type of a variable if you declare and initialize its value simultaneously.
Was this tutorial helpful?