Signup/Sign In

Constants in Go

A constant is a special type of variable that cannot be changed once declared. Constants in Go language can only be of type boolean, number (integer, float, or complex), or string.

Constants are declared like variables only, but by using the const keyword instead of the var keyword. Constants cannot be declared using the short assignment operator.

Declaring/Defining Constant in Go:

The syntax of declaring a constant in Go look like this:

const identifier [type] = value

Here, the identifier is the name of the constant followed by the type of the constant we want to declare and then followed by the value that we want to assign to it.

Following is an example of a declaration:

const PI float = 3.14159

It should be noted that in the case of constants, we can also omit the type if we want, just like we did in variables.

The above code can also be written in a shorter format as follows:

const PI = 3.14159

The above syntax of writing a constant is known as implicit typing.

Typed and untyped constants in Go

Constants that are declared using explicit typing are called typed constants, and the constants that are declared through implicit typing are called untyped constants.

An example of an untyped string constant:

const C1 = 2/3

An example of a typed string constant is:

const name string = "mukul"

The main difference is that when we make use of an untyped constant within a context that requires a typed value, then it becomes a typed in nature.

Constant Evaluation at Compile time

Constants must be evaluated at compile-time, Go doesn't allow constant evaluations at runtime.

It is okay to define a constant as an expression, but all the values that are necessary for the calculation of expression must be available at compile time.

Consider the example shown below:

const C2 = 9/3 // this is valid

The value of C2 was available at compile-time, but the below example will give an error:

const c3 = getConstNumber() // not valid

In the above code example, we are trying to get the value from a function named getConstNumber() and this value can't be provided at compile-time, hence it is invalid in Go.

Multiple assignments for Constants

We can also have multiple constants assignments in a single line. There are different ways of multiple assignments that Go provides.

Let's start with a simple example:

const NAME, PI, DAYS = "Mukul", 3.14159, 7

In the above example, we declared three constants, namely Name, PI, and DAYS. All of the above-declared constants are untyped constants as we did not explicitly mention their types.

Now, let's create multiple typed constants in Go. Consider the example shown below:

const MONDAY, TUESDAY, WEDNESDAY int = 1, 2, 3

All the above-declared constants are typed constants.

What is Constant Overflowing in Go?

Constants in Go do not have any size or sign. They can be of arbitrarily high precision and do not overflow at all. Consider the example shown below:

package main

import (
	"fmt"
)

func main() {
	const PI = 3.14159265358979323846264338327950288419716939937510582097494459
	fmt.Println(PI)
}

In the above example, we declared a variable named PI and assigned the mathematically correct value to it. The Go compiler will loose some precision values, and the output will be the closest float64 value.


3.141592653589793

Conclusion

In the above article, we learned what a constant is in Go, then we learned about the Syntax of a constant in Go, following that we learned about the typed and untyped constants, then we got an idea about how compilation works in Go, following that we learned about multiple declarations of constants, and lastly, we learned about the overflowing condition in Go.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.