Constants in Golang – const keyword

Constants are immutable values with a specific data type.

What can be a constant

Any literal is a constant in Go. Some of the constants in Go would be:

  • Integer: 1234
  • Floating point: 23.56
  • Boolean: true, false
  • Rune: 0x0048
  • Complex: 7+6i
  • String: “Hello”

Declaring a constant in Go – const keyword

The const keyword is used to declare a const value. Once declared, it cannot be reassigned a new value thus making it immutable. A const cannot change its type as well.

package main

import "fmt"

func main() {
	const name string = "John" // declaration
	
	//const b int := 3           // cannot use ":="
	
	//name = "Jane"              // throws error "cannot assign..."

	const p = 22/7             // expression const
	fmt.Println(p)             // prints 3
}

A const cannot be declared using “:=”.

Multiple constants can be declared inside a block. Below is how to do that.

const (
    a int = 42
    b string = "Hi"
)

Type of a constant

Constants have typings and every const if not given the typing is inferred from the values.

1. Typed constant

Typed constants are those whose type has already been declared. Here is an example.

package main

import (  
    "fmt"
)

func main() { 
	const a string = "Hello world"
	fmt.Println(a)
}

In the above example, const a has a type of string as it has been declared.

2. Untyped constant

What are untyped const? They are that const whose type has not been declared. An example is shown below.

package main

import (  
    "fmt"
)

func main() { 
	const b = 42       // type of b is undeclared
	fmt.Println(b)
}

In the code above b has no type declared although it is an integer literal.

The compiler will infer type at compile time. The type will be int.

Constant expressions

Const can have an expression that will then be evaluated to a const value. Let’s see some examples of using expressions as a const.

package main

import (  
    "fmt"
)

func main() { 
	const a = 1 + 2.4
	
	fmt.Printf("%f (%T)\n", a, a) // prints 3.400000 (float64)
	
	const b = 3 + 5
	
	fmt.Printf("%d (%T)\n", b, b) // prints 8 (int)
	
	// const c = 3 + true   // error: mismatched types
}

In the above code %T is used to print the type.

Mismatched types cannot be used. If they both have the same type then it is evaluated to that type.

To change a type in that expression we need to explicitly change its type inside that expression to convert that type to our desirable type. Let’s see how we do that.

package main

import (
	"fmt"
)

func main() {
	const a = 3.14 + 7

	fmt.Printf("%f (%T)\n", a, a)   // prints 10.140000 (float64)

	const b = float64(3) + 7.25 // evaluated to float64
	
	fmt.Printf("%f (%T)\n", b, b)   // prints 10.250000 (float64)
}

Usability of const in Go

A const is used anywhere we need to declare something that can be immutable. That means it won’t be changing value over the course of the program.

Constants help the code to be more maintainable as it forces to keep types immutable as well.