In the Go programming language, a variable represents a named memory location that stores a value.

Variables are not only one of the most fundamental features of Go, but also of just about any programming language. They act as containers for storing data and are assigned to specific memory locations on a computer. The value stored in a variable can be one of many different data types, and can also change during the execution of a program. This not only allows us to maintain data, but also manipulate them in the programs we write. Before we can use a variable in Go, however, it must be declared.

Declaring variables

Under the hood, declaring a variable means telling the computer to reserve some space in memory to store a value. We can accomplish this in Go by providing the variable’s name, data type, and value. Consider the following example:

var language string = "Go"

In the above example, we first used var, followed by the name of the variable (language), then the type (string), then finally the value of the variable itself ("Go"). Since we assigned an identifier (i.e., language) to this value (i.e., "Go"), we can directly access the value of the variable by using the identifier itself:

package main


import "fmt"


func main() {
 var language string = "Go"


 fmt.Println(language)
}

As such, when we run the above code snippet, we see "Go" written to standard output.

Note that since the value of a variable can change during program execution, these values can also be reassigned:

package main


import "fmt"


func main() {
 var language string = "Go"
 language = "Golang"


 fmt.Println(language)
}

When the code above runs, we see "Golang" written to standard output.

Declaring variables (type omitted)

Go also provides a shorthand syntax for declaring and initializing a variable. Compare the previous example declaration with the following:

var language = "Go"

Go is a statically-typed language, meaning that the data type of the variable must be specified at compile-time. However, Go also supports type inference, allowing the compiler to determine the data type of a variable based on the value assigned to it. For this reason, we’re able to omit the type (i.e., string) with this alternative shorthand method.

Declaring variables (shorthand)

Along with the shorthand in the previous example, Go also supports an even shorter syntax for declaring variables. If the variable that isdeclaring is inside a function, you can write the following:

language := "Go"

This would be perfectly valid in Go (you’ll see the value of the variable written to standard output):

package main


import "fmt"


func main() {
 language := "Go"


 fmt.Println(language)
}

However, the following would not be valid syntax due to the declaration occurring outside a function:

package main


import "fmt"


language := "Go"


func main() {
 fmt.Println(language)
}

In the end, all three ways of declaring variables produce the same result, and are largely stylistic choices.

Variable data types

At this point, we’ve seen examples of variable declarations for string values. Go supports several basic data types:

Boolean:

  • bool
    • This refers to either a true or false value

String:

  • string

Numeric:

  • int int8 int16 int32 int64
    • Signed integers (e.g., int8 is a signed 8-bit integer with values from -128 to 127)
  • uint8 uint16 uint32 uint64 uintptr
    • Unsigned integers (e.g., uint8 is an unsigned, or positive, 8-bit integer with values from 0 to 255)
  • byte (alias for uint8)
  • rune (alias for int32)
  • float32 float64
    • Used for numbers with decimals
  • complex64 complex128
    • Used for extremely large numbers

In Go, you may choose these data types, along with other complex data types (e.g., arrays, slices, maps, structs, etc.), to declare your variables.

Learn more about variables

Variables are a fundamental component of Go programs. Rather than entering data directly into a script or some application code, a developer can use variables to represent data. Variables allow us to store, reuse, and manipulate data while our code is running. 

For more information about variables and how to leverage them in the Go programs you write, enroll in Udacity’s Golang course.