In GoLang, there are multiple ways to declare a variable. Let's look at an example below:

package main

import "fmt"

func main() {
	// Default Initialization
	var s int
	fmt.Println("Value of s is: ", s)

	// Shorthand Declaration
	x := 3
	fmt.Println("Value of x is: ", x)

	// Other Declaration Forms
	var y = 5
	var b int = 7
	fmt.Println("Value of y is: ", y)
	fmt.Println("Value of b is: ", b)

	z := x + y
	fmt.Println("Value of z is: ", z)

	name := "Darwin"
	fmt.Println("Value of name is: ", name)

	// Assigning a new value
	z = 5
	fmt.Println("New value of z is: ", z)

	name = "Pogi"
	fmt.Println("New value of name is: ", name)

	// Multiple variables of the same type
	multi1, multi2, multi3 := 100, 200, 300
	fmt.Println("Multi Same Types: ", multi1, multi2, multi3)

	// Multiple variables of different types
	multi4, multi5, multi6 := 400, "five hundred", 600
	fmt.Println("Multi Different Types: ", multi4, multi5, multi6)
}

Go Playground Link

Explanation

Default Initialization

The first example "var s int" is initializes a variable with its zero value or its default value. For numerical types such as int, the default value is zero while, for string, the default value is an empty string "".

Therefore, the result of these lines will be as follows:

// Default Initialization
var s int
fmt.Println("Value of s is: ", s)

var s int
fmt.Println("Value of s is: ", s)

Result:
Value of s is:  0

Shorthand Declaration

x := 3

The line above is a shorthand declaration which assigns the value 3 to the variable x. The compiler, by default, will automatically decide the type for this variable. In this instance, the type will be an int.

Therefore, the result of these lines will be as follows:

// Shorthand Declaration
x := 3
fmt.Println("Value of x is: ", x)
Result:
Value of x is:  3

Other Declaration Forms

// Other Declaration Forms
var y = 5
var b int = 7
fmt.Println("Value of y is: ", y)
fmt.Println("Value of b is: ", b)

var y = 5 is commonly used when initializing a variable in package level or declaring multiple variables.

var b int = 7 is rarely used because the compiler will already assume the type of the variable if the value is defined on the same line. Therefore, there is usually no need to explicitly mention that the type. However, this is vital when the types are different. We will explore this use case later on.

The result of these lines will be as follows:

Value of y is:  5
Value of b is:  7

Result Assignment

It is also possible to immediately assign the result of an operation to a new variable:

z := x + y
fmt.Println("Value of z is: ", z)

In this example, we add the values of x and y which will have the result of 8. The result 8 will the be assigned to the variable z. This results in the following:

Value of z is:  8

Assigning a New Value

z = 5
fmt.Println("New value of z is: ", z)

As can be seen from above, when assigning a value to an existing variable, we only use the = symbol not the :=. The result of this is as follows:

New value of z is:  5

Note: If we use z := 5 on this line, it will result into a compiler error because there already is an existing variable with the name z. An exception to this rule is if the variables are in different levels or scope which will be covered later on.

Another important point is that the type of a variable cannot change. If it is initially set to a type of string then we can only strings to this variable. Assigning a different type will result in an error.


Multiple Variable Assignment

// Multiple variables of the same type
multi1, multi2, multi3 := 100, 200, 300
fmt.Println("Multi Same Types: ", multi1, multi2, multi3)

// Multiple variables of different types
multi4, multi5, multi6 := 400, "five hundred", 600
fmt.Println("Multi Different Types: ", multi4, multi5, multi6)

It is possible to declare and initialize multiple variables in one line using the shorthand declaration. The variables can even be of different types. See the result of the code above below:

Multi Same Types:  100 200 300
Multi Different Types:  400 five hundred 600

Important Notes

  • Shorthand declaration cannot be used to declare a package-level variable (this will be covered in later sections).
  • A variable that is declared must be used. Otherwise, it will result in a compilation error.
  • Go is a strongly typed language. If a variable is initially defined as an int, we cannot assign a string value to it later on.
  • We can declare a variable without initializing its value. The variable will have a default value which is called zero value in Go. If we declare:

var y int

the value of y will be 0 by default. You may refer to this link for the default values for different types.

From the documentation:

Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.