Skip to the content.

Using variables

With variables, we can remember values and later refer to them via named references. using variables will make our code easier to read.

Introduction

In this lesson we’ll cover:

Declare variables

In Go, there are many ways to declare variables:

Assign variables

To assign a new value to a variable, it needs to exist first. You use the assignment operator, =. Here’s an example:

firstName = "Mike"

Data types

There are many data types you can use with Go. They are divided into different categories:

Declare a variable with a type

There are two ways you can declare a variable and give it a type:

String interpolation

Sometimes, you want to be able to write things to the screen and mix different data types doing so. For example, you might want to write, “Customer: Adam has 20$ in his bank account”.

Let’s say then that this information is represented by these two variables:

var (
  customerName = "Adam"
  accountBalance = 20
)

How can you print out the text above? For this purpose, you can use the Printf() function that takes formatters. The idea is that a formatter is an instruction to what a certain type is. By providing this information to Printf(), it’s able to print the type correctly.

Here’s how you can print the example string from before:

fmt.Printf("Customer %s has %d$ on their bank account", customerName, accountBalance)

Above, the %s represents a string and %d represents a number. By using these formatters as placeholders, the variables are correctly implemented, and the output becomes:

Customer Adam has 20$ on their bank account

Assignment - define some variables and print them out

Define some variables you might need for the card game Texas Holdem and print them out.

Create a file main.go and give it the following content:

    package main
    
    import "fmt"
    
    func main () {
    }
  1. Add the following variables after the import section:

    var (
      players = 3
      replay = false
      namePlayerOne = "chris"
    )
    

    Now you have:

    • players, to represent the number of players in the game.
    • replay, a boolean stating whether to start a new game session when the old one has ended.
    • namePlayerOne, a string representing the name of the first player.

    All of these variables help describe essential information in a Texas Holdem game.

    Next, let’s run our app to make sure it works.

  2. Add the following code to the main() function to print out the variables:

    fmt.Println(players)
    fmt.Println(replay)
    fmt.Println(namePlayerOne)
    
  3. Run go run main.go in the terminal:

    go run main.go
    

    You should see the following output:

     3
     false
     chris
    

    Great, you now have a starting point for an app you can keep building on.

🚀 Challenge

See if you can come up with more variables to represent the state in a Texas Holdem card game, like for example, other players, the card deck etc. What data type would you give those variables?

Review & Self Study

Have a look at this official tutorial on variables using a Go sandbox

Solution

package main

import "fmt"

var (
  players = 3
  replay = false
  namePlayerOne = "chris" 
)

func main () {
  fmt.Println(players)
  fmt.Println(replay)
  fmt.Println(namePlayerOne)
}