Search⌘ K
AI Features

Constants

Explore how to define and use constants in Go, including explicit and implicit typing, typed and untyped constants, compile-time evaluation, multiple assignments, and creating enumerations with iota for clearer, efficient code.

Introduction

A value that cannot be changed by the program is called a constant. This data can only be of type boolean, number (integer, float, or complex) or string.

Explicit and implicit typing

In Go, a constant can be defined using the keyword const as:

const identifier [type] = value

Here, identifier is the name, and type is the type of constant. Following is an example of a declaration:

const PI = 3.14159

You may have noticed that we didn’t specify the type of constant PI here. It’s perfectly fine because the type specifier [type] is ...