Variables in Golang6 min read

What are variables in golang?

Variables in golang are used to store a value. Basically, declaring a variable allocates a memory location in Random Access Memory to store a value of a particular data type. It is useful when the user is entering a value in a program. This value should be saved in a specified variable so that it can be properly utilized throughout the program. The value of a variable can be altered at any time throughout the program’s execution.

Naming convention and rules for variables in golang

  1. Variable name must begin with a letter or an underscore (_). Variable name may contain ‘a-z’, ‘A-Z’, and ‘0-9’, and even ‘_’. For example, Hiberstack, hiberStack, HiberStack123, _hiberStack12
  2. A variable name should not start with a number
  3. Variable names are case sensitive
  4. Keywords are not allowed to be used as variables in golang. For example, var cannot be used as a variable
  5. As such there is no limit for the length of a variable

Declaring and Initializing a variable

There are 2 ways to declare/initialize a variable,

1. Using var keyword

We can declare a variable using a var keyword which requires us to provide the data type of the variable as well. We can even initialize a variable with var keyword without providing the datatype and directly assigning a value to the variable.

Syntax:

  • Declaring a variable – var variable_name datatype
  • Initializing a variable –
    1. var variable_name datatype = value
    2. var variable_name = value

Example:

// Go program to illustrate
// concept of variable
package main

import "fmt"

func main() {
  var a1 int
  var b1 int = 11
  var c1 = "hello"
  
  fmt.Println("The value of a1 is : ", a1)
  fmt.Println("The value of b1 is : ", b1)
  fmt.Println("The value of c1 is : ", c1)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run VariableDemo1.go
The value of a1 is :  0
The value of b1 is :  11
The value of c1 is :  hello
declaring a variable in golang

As you can see from the output, the value of a1 is 0. This is because there is no concept of an uninitialized variable in Go. An int variable will be assigned 0 if no value is assigned to it. For float, 0.0. For String, null. And for Boolean, a false value will be assigned.

We can even declare multiple variables at a time in go.

Example:

// program to declare multiple variables at a time
package main

import "fmt"

func main() {
  var x1, x2, x3 int
  var y1, y2 int = 11, 21
  var z1, z2 = "hello", "world"
  
  fmt.Println("The value of x1 is : ", x1)
  fmt.Println("The value of x2 is : ", x2)
  fmt.Println("The value of x3 is : ", x3)
  fmt.Println("The value of y1 is : ", y1)
  fmt.Println("The value of y2 is : ", y2)
  fmt.Println("The value of z1 is : ", z1)
  fmt.Println("The value of z2 is : ", z2)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run VariableDemo2.go
The value of x1 is :  0
The value of x2 is :  0
The value of x3 is :  0
The value of y1 is :  11
The value of y2 is :  21
The value of z1 is :  hello
The value of z2 is :  world

We can also initialize multiple variables with different data types at once. For this, we don’t have to mention the data type while initializing the variables.

Example:

// Go program to illustrate initialization of
// multiple variables with differnet data types

package main

import "fmt"

func main() {
	var p1, p2, p3, p4 = 11, "hiberstack", 75, 1234.56

	fmt.Println("The value of p1 is : ", p1)
	fmt.Println("The value of p2 is : ", p2)
	fmt.Println("The value of p3 is : ", p3)
	fmt.Println("The value of p4 is : ", p4)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run VariableDemo3.go
The value of p1 is :  11
The value of p2 is :  hiberstack
The value of p3 is :  75
The value of p4 is :  1234.56

2. Using shorthand variable declaration

We can declare or rather initialize a variable directly without the var keyword.

Syntax: variable_name := value

Note -> := is used to initialize a variable whereas = is used to assign a value to a variable after it has been declared.

Points to remember:

  1. The value assigned to a variable determines its data type.
  2. Most of the time, the variables are declared using short method because of its flexibility.
  3. Using short variable declaration we can define multiple variables at once.
  4. Short variable declaration is generally used when we want to initialize the variable at the time of declaration itself whereas var keyword is used when there is a need to assign a value to the variable in the later part of the program.

Example:

// Go program to illustrate
// concept of variable
package main
import "fmt"

func main() {
  // Using short variable declaration
  // Multiple variables of same types
  // are declared and initialized in
  // the single line
  a1, a2 := 100, 46

  // Display the value and type of the variables
  fmt.Println("The value of a1 is : ", a1)
  fmt.Printf("The type of a1 is : %T", a1)

  fmt.Println("\nThe value of a2 is : ", a2)
  fmt.Printf("The type of a2 is : %T", a2)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run VariableDemo4.go
The value of a1 is :  100
The type of a1 is : int
The value of a2 is :  46
The type of a2 is : int

We can also initialize multiple variables with different data types in a single declaration. The value supplied to these variables will determine their data type.

Example:

// Go program to illustrate
// concept of variable
package main
import "fmt"

func main() {
  // Using short variable declaration
  // Multiple variables of different types
  // are declared and initialized in the single line
  x1, x2, x3 := 100, 46.87, "Hiberstack"

  // Display the value and type of the variables
  fmt.Printf("The value of x1 is : %d", x1)
  fmt.Printf("\nThe type of x1 is : %T", x1)

  fmt.Printf("\nThe value of x2 is : %f", x2)
  fmt.Printf("\nThe type of x2 is : %T", x2)

  fmt.Printf("\nThe value of x3 is : %s", x3)
  fmt.Printf("\nThe type of x3 is : %T", x3)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run VariableDemo5.go
The value of x1 is : 100
The type of x1 is : int
The value of x2 is : 46.870000
The type of x2 is : float64
The value of x3 is : Hiberstack
The type of x3 is : string

Share:

Leave a Reply