A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

Variables and Constants in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

In this chapter we will discuss how variables and constants can be used in Go programming language.

Variables in Golang

A variable is a name given to storage area that a program can read or write.

In Golang, variables are either declared using the var statement or the := annotation.

A variable can be scoped to entire package or a specific function.

Program to demonstrate package level variables in Golang

Program given below demonstrates how package level variables can be defined using the var statement.

Package level variables can be accessed from anywhere inside the package.

package main

import "fmt"

var hello string = "hello"

var world = "world"

var a, b, c int = 1, 2, 3

var x, y, z = 4, 5, 6

func main() {
	fmt.Println(hello)
	fmt.Println(world)

	fmt.Println(a,b,c)
	fmt.Println(x,y,z)
}

Program output

Above program produces following output:

hello
world
1 2 3
4 5 6

Program to demonstrate function level variables in Golang

Program given below demonstrates how function level variables can be defined using the var and := annotation.

Function level variables can not be accessed outside the area of the function itself.

package main

import "fmt"

func main() {

	var hello string = "hello"

	var world = "world"

	var a, b, c int = 1, 2, 3

	var x, y, z = 4, 5, 6

	// variable declaration using the shorthand `:=` annotation
	something := "something"

	fmt.Println(hello)
	fmt.Println(world)

	fmt.Println(a, b, c)
	fmt.Println(x, y, z)

	fmt.Println(something)
}

Program output

Above program produces following output:

hello
world
1 2 3
4 5 6
something

Variables without initializers in Golang

In above programs, all variables are defined using initializers, but if you don’t want to initialize a variable instantly and instead want to initialize it later on then refer to the following program to understand how it is done:

package main

import "fmt"

// declare variable a
var a int

func main()  {
	// declare b
	var b int

	// initialize variables later on
	a=1
	b=2

	// print values
	fmt.Println(a)
	fmt.Println(b)
}

Program output

Above program produces following output:

1
2

Variables with zero values

If a variable is declared but not initialized then it is given a zero value by the Go compiler. Program given below demonstrates how variables are given zero value when they are declared but not initialized.

package main

import "fmt"

func main()  {
	// variables declared but not initialized
	var a int
	var b string
	var c rune
	var d bool
	var e float32
	var f byte
	var g [2]int
	var h []string

	// printing zero value of variables
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
	fmt.Println(d)
	fmt.Println(e)
	fmt.Println(f)
	fmt.Println(g)
	fmt.Println(h)
}

Program output

Above program produces following output:

0

0
false
0
0
[0 0]
[]

Constants in Golang

A constant is a fixed value that a program can not alter during the course of execution of a program. Value of a constant can not be modified during the course of execution of a program.

Program to demonstrate package level and function level constants

const keyword can be used for defining package level and function level constants in Golang.

Refer to the program given below that demonstrates how to define constants using the const keyword.

package main

import "fmt"

// packge level constants
const hello string = "hello"

const world = "world"

const a, b, c int = 1, 2, 3

const x, y, z = 4, 5, 6

func main() {
    // function level constant
	const something = "something"

	fmt.Println(hello)
	fmt.Println(world)

	fmt.Println(a,b,c)
	fmt.Println(x,y,z)

	fmt.Println(something)
}

Program output

Above program produces following output:

hello
world
1 2 3
4 5 6
something