Go Variables & Constants Tutorial
In this Go tutorial we learn about temporary data containers called variables and constants.
We learn how to define variables and constants with certain data types, how to initialize them with values, how to change their values later on and how to use them.
We also learn how to create variables and constants without specifying its type by using the := operator.
What is a variable
A variable is a data container that stores the application’s temporary data in memory while it runs.
As an example, consider a calculator app. When we use the calculator, the program stores the result in a variable and we can use the result to perform additional calculations.
3 + 5 = x
x * 8 = y
In the example above, the letters ‘x’ and ‘y’ store the resulting data from the arithmetic.
These types of variables are used everywhere within an application and allow us to store data that’s only needed while the program is running.
When the application quits, the variables are cleaned and the space reserved for them in memory (RAM) is released back to the system.
How to create (define) a variable
To create a variable we use one of Go’s special keywords, var , followed by an identifier (a name for our variable) and a data type.
The var keyword tells the compiler that we want to define a variable with the following name.
var pi float32
In the syntax example above, the variable’s name is pi and it is of type float32.
When the variable exists, we can assign a value to it by using the assignment operator ( = ).
var pi float32
pi = 3.14
We’re telling the compiler that the variable pi has the value of (it equals) 3.14.
How to create (initialize) a variable with a value
We can create a variable that holds a value when it’s defined. This is known as initialization and simply adds the value to the variable with the assignment operator.
var pi float32 = 3.14
Instead of first defining the variable and adding a value to it later, we do it all in one step.
How to initialize a variable without a type declaration
We can initialize a variable without specifying a type in Go. This is known as dynamic type declaration and the compiler will infer the data type based on the value of the variable.
To do this we remove the var keyword and the data type from the statement. We also use the := operator instead of the traditional = operator.
variable_name := value
package main
func main() {
// Init with type
var num1 byte = 8;
// Init without type
num2 := 8;
}
How to use a variable
We use a variable by referring to its name within our program.
package main
import "fmt"
func main() {
// init variable
msg := "Hello, World!"
// use variable
fmt.Println(msg)
}
In the example above, we create a variable called msg that contains the words ‘Hello, World!’.
Then, we use the variable in the Println() function to print whatever is in the variable to the console.
How to change the data in a variable
Variables in Go are mutable, which means we can change the values inside the variables when the application is running (runtime).
To do this we assign a new value to an existing variable with the traditional assignment operator ( = ).
package main
import "fmt"
func main() {
var num1 byte = 8
fmt.Println(num1)
// change value
num1 = 5
fmt.Println(num1)
}
In the example above, we initialize our variable with the value 8, but then later change the value to 5 through assignment.
What is a constant
A constant is a variable that cannot have its value changed when the program is running. Constants are also known as literals.
Constants are used when we don’t want a value to be accidentally changed during runtime, like the value of Pi, or gravity.
How to initialize a constant
A constant is defined like a variable, except that we use the const keyword instead of the var keyword.
A constant must also be initialized with a value because a value cannot be added later on.
const PI float32 = 3.14
The convention in Go, and indeed most programming languages, are to name constants with only capital letters.
If multiple words make up the identifier, the words are separated with an underscore.
Summary: Points to remember
- A variable is a temporary data container while the app runs.
- Variables are declared with the var keyword.
- A variable that’s defined, or initialized with the = operator, must declare its data type explicitly.
- A variable can be initialized with type inference with the := operator.
- A variable is used by referring to its name.
- A constant is a variable that cannot change its value at runtime.
- Constants are initialized with the const keyword.
- Constants must be initialized with a value.