W3Basic Logo

Go Variables and Constants

A variable is a container that holds a specific type of data that can be further referenced and manipulated during the execution of the program.

Constant on the other hand is similar to variable that holds specific type of data which can be referenced but we cannot change or manipulate the value of constant during the execution of the program.


Use of Variables and Constants in Go

Consider the following code, which prints "Hello, World!" three times:

package main

func main() {
	println("Hello, World!")
	println("Hello, World!")
	println("Hello, World!")
}

If you want to change the message, you will have to edit all three lines, which is not very convenient. It would be preferable to store the message in a variable and then use it in the println function:

package main

func main() {
	var message string = "Hello, World!"
	println(message)
	println(message)
	println(message)
}

Even better, since the message will never change, we can use a constant instead of a variable:

package main

func main() {
	const message string = "Hello, World!"
	println(message)
	println(message)
	println(message)
}

Note: To print multiple values, you can also pass them as separate arguments to the println function, but no new line will be added between them.

That is, in essence, what variables and constants are: a way to store a value that can be used later in the program. Let us see them in more detail.

Variables

A variable is a named storage location that can hold a value of a specific type that can be changed during the execution of the program.

There are three ways to define a variable in Go:

  • using the var keyword with an explicit type (e.g. var year int = 2022)
  • using the var keyword with an implicit type (e.g. var year = 2022),
  • using the := operator (e.g., year := 2022).

In the last two cases, the type is inferred from the value on the right side of the assignment operator.

It is a matter of personal preference which of the three ways to use. The first one is the most explicit, the third one is the most concise, and the second one is a mix of the two.

Declaring multiple variables or constants at once

Let us also see how to define multiple variables or constants at once. You can use all three syntaxes we have seen so far:

Syntax 1

var year, month, day int = 2022, 1, 1

Syntax 2

var year, month, day = 2022, 1, 1

Syntax 3

year, month, day := 2022, 1, 1

For readability (or in the case all variables don't have the same type which is required for the first syntax), you can also define them on separate lines:

var (
	year  int = 2022
	month int = 1
	day   int = 1
)

Constants

A constant is a named storage location that can hold a value of a specific type that:

  • is known at compile time,
  • cannot be changed during the execution of the program.

To declare a constant, you use the const keyword:

const message string = "Hello, World!"

Note: Just like variables, constants can be defined with an implicit type or on multiple lines, but there is no equivalent of the := operator.

Constants are useful in a variety of situations, for example, to store the value of:

  • a configuration parameter (e.g. the name of a file),
  • a mathematical constant (e.g. math.Pi),
  • a unit of measurement (e.g. time.Second).

You will discover yourself other use cases as you write more Go code.

© 2023 W3Basic. All rights reserved.

Follow Us: