Intro
Constants are declared like variables, but with the const keyword. Constants can only be character, string, boolean, or numeric values and cannot be declared using the := syntax. An untyped constant takes the type needed by its context.
Example:
const Pi = 3.14
const (
StatusOK = 200
StatusCreated = 201
StatusAccepted = 202
StatusNonAuthoritativeInfo = 203
StatusNoContent = 204
StatusResetContent = 205
StatusPartialContent = 206
)
Constantsbelong tocompile time. They must be initilized with value when they are declared.Constantsare created atcompile time. In therun time, Go just transforms it into avalue.Unnamed constants: All
basic literalsareunnamed constants. Following are examples ofbasic literals:// Unnamed constants 1 3.14 "hello" true falseNamed Constants: All
named constantswill be replaced to theirvaluesinruntime. They need to bedeclaredfirst.Untyped Constants:
Constantsmay or may not have a type.If the value is’nt going to change throughout our program’s lifetime and we already know the value (if it belongs to compile time) then we should go for
named constants.Constants are
immutablei.e. we cannot change their values.We
cannot initializea constant to aruntime value.We can use
expressionswhile initializingconstants.
Constant Types
We can declare constants using
non-numerictypes as well.Constants don’t have to be only
numeric values.We
don't have todeclare thetypesof constants.For e.g.
func main() { // Below works.. const min int = 1 const pi float64 = 3.14 const version string = "2.0.3" const debug bool = true // Declaring constants without types also works. const min = 1 const pi = 3.14 const version = "2.0.3" const debug = true // We can use expressions while initializing constants. const min = 1 + 1 //2 const pi = 3.14 * min // 6.28 const version = "2.0.3" + "-beta" // 2.0.3-beta const debug = !true // false }
Multiple Constants Declaration
Constantsget theirtypesandexpressionsfromthe previous constant.We can declare
multiple constantsin a single go as below:func main() { // Multiple constants of same type in one go const min, max int = 1, 1000 // Declaring in group const ( min int = 1 max int = 1000 ) // Constants get their types and expressions from the previous constant const ( min int = 1000 // 1000 max // 1000 ) }
Typeless Or Untyped Constants
When we declare
a constant without a type, it becomesuntyped constant (typeless constant).All
basic literalsare alsotypeless. They all aretypeless constant values.A
constant with a typecan only be used witha valueofthe same type.The
untyped numeric constantcan be used withall numeric valuestogether.For e.g.
func main() { const min = 42 var i int = min // Type of constant 'min' = int var f float64 = min // Type of constant 'min' = float64 var b byte = min // Type of constant 'min' = byte var j int32 = min // Type of constant 'min' = int32 var r rune = min // Type of constant 'min' = rune }
Default Types
Conversiononly happens whena type is needed.Go
convertsatypeless constantto atypedvalue when atypeis needed.For e.g.
func main() { const min int32 = 1000 max := 5 + min // Type of 'max' is 'int32' // Internally this happens: max := int32(5) + min }Go
implicitly convertsthetypeless constantto atyped value.For e.g.
func main() { const min = 1000 max := 5 + min // Type of 'max' is 'int' // Internally this happens: max := int(5) + int(min) }An
untyped constanthas adefault type.Go
evaluates the expressionthen itconvertstheresulting typeless valueto itsdefault value.
IOTA
IOTAis nothing but anumber generatorforconstants. In other words, it is ever increasing automatic counter.IOTAis built-inconstant generatorwhichgenerateseverincreasing numbers.IOTAstarts at0.We can use
expressionswithIOTA. So, the otherconstantswillrepeattheexpressions.We can use blank identifier (_) to adjust the values of constants:
func main() { const ( EST = -(5 + iota) // -5 _ // -6 | Discarded/skipped due to blank identifier MST // -7 PST // -8 ) }