Go Variables

Variables are used to store information temporarily to be referenced and manipulated in a computer program.

In Go, a variable stores a value of the specific data type. It can store a numeric, boolean, string, or other types of value.

Variable Declaration and Initialization

There are two ways to declare a variable in Go lang:

  1. Using var keyword
  2. Using the shorthand syntax

Using var keyword

The most common method of describing a variable in Go lang is by using the keyword var.

Syntax:
var variable-name datatype = value

The following declares and initialize variables of different data types.

Example: Declare and Initialize Variables
var num int = 20 // int variable
var amount float32 = 49.99 // float variable
var isValid bool = true // bool variable
var name string = "Yash"// string variable

fmt.Println(num, amount, isValid, name) // output: 20 49.99 true Yash

If you declare a variable and assign a value in the same line, then you can omit the type. The type will be implicitly taken from the assigned value.

Example: Declare and Initialize Variables without DataType
var num = 20 
var amount = 49.99 
var isValid = true
var name = "Yash"

fmt.Println(num, amount, isValid, name) // output: 20 49.99 true Yash

Variables can be declared without assigning a values. The initial value is set to the default value of the datatype, as shown below.

Example: Variable Declaration without Initialization
var num int
var amount float32
var isValid bool
var name string

fmt.Println(num, amount, isValid, name) // output: 0 0 false

Note: The default value of int type is zero, string type is "", and bool is false.

Variables can be declared first and initialized later.

Example: Late Initialization
var num int
var amount float32
var isValid bool
var name string

num = 20
amount = 49.99
isValid = true
name = "Yash"

fmt.Println(num, amount, isValid, name) // output: 20 49.99 true Yash

Multiple variables can be declared and initialized in the same line.

Example: Late Initilization
var num1, num2 int = 20, 30 //int variables
var amount, name = 49.99, "Yash" // float and string variables

fmt.Println(num1, num2, amount, name) // output: 20 30 49.99 Yash

Variables are case sensitive which means that the variable num is different from the variable Num, NUM, and NuM.

Using the shorthand syntax

Use := to declare and assign a value to a variable without using the var keyword with implicit type.

Example: Short Variable Declaration
num := 20
amount := 49.99
isValid := true
name := "Yash"

Multiple variables can also be declared using shorthand syntax.

Example: Declare Multiple Variables
num1, num2 := 20, 30 //int variables
amount, name := 49.99, "Yash" // float and string variables

fmt.Println(num1, num2, amount, name) // output: 20 30 49.99 Yash

Variable Scope

In Go, Variables in Go can be declared at the package level or the function level. Look at the following example.

Example: Scope of Variables
package main
import "fmt"
    
var num = 100

func main () {
	fmt.Println(num) // output: 100

	var greet = "Hello"
	fmt.Println(greet) // output "Hello"
	
	test()
}

func test() {
	fmt.Println(num) // allowed to access package level variable
	//fmt.Println(greet) //error
}

In the above example, Here, the num variable is declared directly in the package, so it has a package-level scope. The greet variable is declared in the main() function, so it has a function-level scope. The num variable with the package scope variable can be accessed from anywhere in the package e.g. in the main() and test() functions. The greet variable is declared in the main() function which can only be accessed in the main() function.

Variable Naming Conventions

  • Variable names must begin with a letter or an underscore. A variable name can include a to z, A to Z, digits 1 to 9, and '_'.
  • A variable cannot begin with a number.
  • A variable name cannot use Go keywords as variable names.
  • A variable name can only contain alphanumeric characters and underscore.
  • A variable name cannot contain spaces.