Skip to main content

Go Variables

A variable is a named place in your program where a value is kept, so you can use it again later.


Go Variable Types

A variable's type decides what kind of data it can hold. Go has several types, including these:

  • int - whole numbers (integers), like 123 or -123
  • float32 - floating point numbers, which have decimals, like 19.99 or -19.99
  • string - text, like "Hello World", written inside double quotes
  • bool - one of two states, true or false

Declaring (Creating) Variables

Go gives you two ways to declare a variable. The syntax for each one is shown below.

1. The var keyword:

Write var, then the name of the variable and its type.

Note: You can leave out the type or the value, but never both.

2. The := sign:

Write the variable name, then :=, then the value.

Note: With this form the type is inferred: the compiler works it out from the value you provide.

Note: Because the compiler needs that value, := can't be used to declare a variable without assigning one.

Var Syntax syntax

main.go
var variablename type = value

Short Syntax syntax

main.go
variablename := value

Variable Declaration With Initial Value

When you already know what a variable should hold, you can do the declaration and the assignment in a single line, as the example below shows.

Note: Neither student2 nor x has a type written in the code, so their types are inferred from their values.

Example

Prompt
Declare variables using var with type, var with inferred type, and := shorthand. Print all three.
Output: Go program and captured output

Variable Declaration Without Initial Value

Go never leaves a variable uninitialized. Declare one without giving it a value, and it automatically receives the default value for its type:

Example

The program below declares three variables, a, b and c, and gives none of them a value. Running it shows that each one already holds the default for its type: a is "", b is 0 and c is false.

Prompt
Declare string, int, and bool variables without values and print their defaults.
Output: Go program and captured output

Value Assignment After Declaration

You don't have to give a variable its value straight away. You can declare it first and assign the value later, which is useful when you don't know the value yet.

Note: This only works with var. Using ":=" always requires a value as part of the declaration.

Example

Prompt
Declare a string variable, assign a value after declaration, and print it.
Output: Go program and captured output

Difference Between var and :=

var and := differ in a couple of small ways:

You can use var both inside and outside functions, and you can declare the variable and assign its value in separate steps.

:= works only inside functions, and the declaration and assignment cannot be separated: they have to happen on one line.

Example

Here, var declares variables outside any function, which works without a problem:

Prompt
Declare variables outside a function with var and print them inside main.
Output: Go program and captured output

Example

This program tries to use := outside a function, so it fails with an error instead of running.

Prompt
Show the error when using := outside a function in Go.
Output: Go program and captured output