Constants
This lesson discusses how constants are used to store data values in Go.
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 optional because the compiler can implicitly derive the type from the value. Let’s ...