Go Constants:
Constants are fixed values that cannot be changed during the execution of a program. They are declared using the const keyword in Go.
Basic Constant Declaration
Constants can be declared individually or in groups. Here's a simple example:
go
const Pi = 3.14
const Greeting = "Hello, Go!"Grouped Constants
You can declare multiple constants together using parentheses:
go
const (
A = 1
B = 2
C = 3
)Untyped Constants
Go allows untyped constants that take their type when assigned to a variable:
go
const x = 42
var y int = xUsing iota
iota is a built-in identifier used to simplify the definition of incrementing constants.
go
const (
First = iota
Second
Third
)Key Points
- Constants are evaluated at compile-time.
- They cannot be changed once declared.
iotahelps generate sequences easily.- Typed and untyped constants both exist.