Go Constants

In this tutorial, you will learn about constants in the Go programming language and how to create them with the help of examples.


As the name suggests constants denote the value of something which cannot be changed. In programming, constants are the same as variables except that they cannot be changed during the execution of the program.

Go constants are one of the fundamental and essential parts of the Go programming language. Constants in the Go programming language are the same as variables but the values are fixed and the value remains the same during the entire program execution.

  • Constants can be any of the basic data types such as integer, float, character, string, etc.
  • Constants are also called literals.
  • const declares a constant value.

Following are some of the example of constants.

25
41.25
"Go Tutorial"

Declaring a constant: The const keyword

The keyword const is used to declare a constant in Go. Following is a simple example of declaring a constant.

package main

import (
"fmt"
)

func main() {
     const pi = 3.14 //declaring a constant
     fmt.Println(pi)
}

In the above example, pi is a constant and the value of the constant is 3.14.

Declaring multiple constants

It is also possible to declare multiple constants in a single statement. Let’s check how to declare multiple constants in a single statement with the help of an example.

package main

import ( 
"fmt"
)

func main() { 
const (
     name = "Sagar"
     age = 30
     height = "5 feet 7 inches"
) //declaring multiple constant
fmt.Println(name)
fmt.Println(age)
fmt.Println(height)

}

In the above program, we have declared 3 constants name, age, and height. Once executed, it will produce the following result.

Output

Sagar
30
5 feet 7 inches

Once a value of a constant is assigned, you cannot reassign the value. In the following example, we have already assigned the value of the constant pi to 3.14. If we assign a new value 5.25 again to the constant pi and try to compile the program, the compiler will throw an error saying that cannot assign to pi (declared const). This is because the variable pi is a constant and the value cannot be reassigned again.

package main

import (
"fmt"
)

func main() {
     const pi = 3.14 //valid as first time assignment
     pi = 5.25 //reassignment not allowed
     fmt.Println(pi)
}

Another important point is that constant value cannot be assigned at the run time. This is because that constant value always realized at the compile-time, not in the run time. Hence, it is not possible to assign a constant value using the function call as the function call takes place at the run time.

package main

import (
"fmt"
"math"
)

func main() {
var a = math.Pow(2, 4) //allowed
const b = math.Pow(2, 4) //not allowed
fmt.Println(a)
}

In the above program, a is a variable and the value of a can be assigned with the function math.Pow(2, 4). We will learn about function in the later chapter.

On the other hand, b is a constant and the value of the b should be realized during the compile time. But the expression math.Pow(2, 4) will assign the value only at the run time which is not allowed. So the above program will give the compile error as below.

./prog.go:10:8: const initializer math.Pow(2, 4) is not a constant

String constants

String constants are generally denoted by the double quotes and anything between the double quotes denotes the string constant. Follow are some of the examples of string constants.

"Hello World"
"Tutorialsbook"
"Sagar"

Typed and Untyped constants

Untyped constant

Constant which does not has any type associated with it called Untyped constant. Let’s check the below example to understand the concept.

constant hello = "Hello World"

In the above example, a constant named hello with the initial value "Hello World" has been declared. However, no type has been declared here. This type of variable is called the Untyped constant.

Typed constant

Constant which has a type associated with it called Typed constant. Let’s check the below example to understand the concept.

constant hello string = "Hello World"

In the above example, the constant hello has been initialized with the value "Hello World" and the type of the constant is a string. This type of constant is called the Typed constant.

Go is strongly typed language. All the variable requires an explicit type.

All the untyped constants have a default type associated with them. They supply the type only if the line of code demands it.

package main

import (
“fmt”
)

func main() {
const a = “Sagar”
var name = a
fmt.Printf(“type %T value %v”, name, name)

}

In the above example, the statement const a = "Sagar" defines an untyped constant a. In the next line of code, the constant has been assigned to the variable name. In this case, the default type associated with the untyped constant a will be assigned to the variable name. This program gives the following output.

type string value Sagar

Boolean Constants

Boolean constants are the same as the string constants and apply the same rules as the string constants. The only thing is that Boolean constants have two untyped constants true and false.

package main

import "fmt"

func main(){
const trueConst = true

// Type definition using type keyword
type myBool bool 
var defaultBool = trueConst // allowed
var customBool myBool = trueConst // allowed

// defaultBool = customBool // not allowed
fmt.Println(defaultBool)
fmt.Println(customBool) 
}

Output

true
true

Numeric Constants

Numeric constants include integer, float, and complex constants.

Let’s understand the concept of numerical constant with the help of an example.

package main

import "fmt"

func main() { 
const a = 10
var intVar int = a
var int32Var int32 = a
var float64Var float64 = a
var complex64Var complex64 = a
fmt.Println("intVar",intVar, "\nint32Var", int32Var, "\nfloat64Var", float64Var, "\ncomplex64Var",complex64Var)
}

In the above example, constant a is an untyped constant. The variable a can represent an integer, float, or even a complex number with no imaginary part. Hence it is possible to assign this constant to any of the compatible type variables. The statement var intVar int = a requires a to be an integer type, hence it becomes an integer. Similarly, the statement var float64Var float64 = a requires a to be a complex number hence it becomes a complex number.

Output

intVar 10
int32Var 10
float64Var 10
complex64Var (10+0i)

Please get connected & share!

Categories

Advertisement