Go Variables, Types, And Pointers

You learned how to write a ‘Hello World’ program in the introduction of this Go Tutorial. After that, you learned how to install Go and create and build your Go program. So far, we have only glanced at the language and its syntax! In this next tutorial, we’ll look at some of the essential Go language elements:

  • Variables
  • Pointers
  • Types

We’ll do this by extending our hello.go file from the introduction with some nifty new functionality!

What you should know

Before you read this tutorial, you should be familiar with the basic structure of a Go program, as you learned in the introduction of this Go Tutorial. You should also know how to create and build your Go program. You have a working Go installation and an editor or IDE to edit your Go files.

Variables

A variable is used to store information that can be referenced later on. So a variable is what we use to name the result of, for example, a calculation we make. In other words, we can assign the result of that calculation to a variable.

We can also use variables to store other types of values, like a piece of text (called a string). A variable has three important properties:

  1. a name,
  2. a type,
  3. and a value.

The type of a variable determines the kind of data that it can hold, such as a number (integer) or a string. The value is the data we store in the variable.

We can create an unlimited amount of variables; we just have to make sure we give them unique names.

Declaring a variable

To create a variable, you must first choose a name for the variable and specify its type. For example, to create a variable called name that holds a string value, you use the following code:

var name stringCode language: Python (python)

Once you have declared the variable, you can assign a value to it using the assignment operator (=). For example, to assign the name "John" to the name variable:

var name string
name = "John"Code language: Python (python)

This syntax is more verbose than it needs to be. Since we directly assign “John” to name, we can do it in one line as well:

var name string = "John"Code language: Python (python)

Using the variable

We can now use the variable simply by using its name. Let’s extend our ‘Hello World’ program:

package main

import "fmt"

func main() {
	var name string = "John"
	fmt.Println("Hello", name)
}Code language: Python (python)

Let’s also declare and use a variable holding the person’s age. Non-fractional numbers in Go are stored in variables of type integer. There are several variations of this type, but for now it suffices to use the type called int, which is a machine-dependent type. E.g., on a 64-bit machine, an int can hold larger values than on a 32-bit machine.

Here’s our hello.go file with the extra age variable:

package main

import "fmt"

func main() {
	var name string = "John"
	var age int = 20

	fmt.Println("Hello", name)
	fmt.Println("Your age is", age)
}Code language: Python (python)

Try and run the code and play around with it for a bit, e.g., you could try changing the variable names and their contents.

Asking for input

So far, our variables are not that useful. After all, we could have printed the same information without using variables. Let’s fix that by asking the user for some input. For this, we use another function from the fmt package, called Scan:

package main

import (
	"fmt"
)

func main() {
	// Declare empty variables to hold the user's name and age
	var name string
	var age int

	fmt.Print("Enter your name: ")
	fmt.Scan(&name)

	fmt.Print("Enter your age: ")
	fmt.Scan(&age)

	fmt.Println("Hello", name)
	fmt.Println("Your age is", age)
}Code language: Python (python)

If you pay close attention, you will ask yourself what the ampersands are doing before our variable names. To understand this, we need to dive into pointers.

Pointers

To be continued…

Other topics:

  • golang variable naming convention
  • Global variables (scope)