Utilizing Go Constants
Explore how to declare and use constants in Go for fixed values. Understand typed and untyped constants, create enumerations with iota, and improve enumerator readability using code generation with stringer tools for better logging and debugging.
We'll cover the following...
Constants provide values that are set at compile time and cannot change. This is in contrast to variables, which store values that can be set at runtime and can be altered. This provides types that cannot accidentally be changed by a user and are allocated for use in the software on startup, providing some speed advantages and safety over variable declarations.
Constants can be used to store the following:
Booleans
Runes
Integer types (
int,int8,uint16, and so on)Floating-point types (
float32/float64)Complex data types
Strings
In this lesson, we will discuss how to declare constants and use them in our code.
Declaring a constant
Constants are declared using the const keyword, as ...