Golang Const, Var Examples: Iota

Use the const and var keywords. Generate constants with the iota enumerator.
Consts. Some things do not change. Over the years, a mountain remains mostly the same. Consider a value like 155. This is a constant value: we can use a const block for it.
Constant creation. With iota we gain a way to generate groups of constants based on expressions. With it, we can create an entire group of constants. This reduces bugs.
An example. Here we introduce the const keyword in a block of enumerated constants. We do not use all the features of const here. We declare three names and give them values.

Tip: The equals signs should line up vertically in consts. This is achieved with the Gofmt program.

Tip 2: We can use these constants as values in a Go program by directly inserting the names.

Golang program that uses const package main import "fmt" const ( Cat = 10 Dog = 20 Bird = 30 ) func main() { // Use our constants. fmt.Println(Cat) fmt.Println(Dog) fmt.Println(Bird) } Output 10 20 30
Iota. This is an enumerator for const creation. The Go compiler starts iota at 0 and increments it by one for each following constant. We can use it in expressions.

Caution: We cannot use iota in an expression that must be evaluated at runtime—a const is determined at compile-time.

Tip: The term iota is like beta: it stands for the letter "I" in Greek, just as beta stands for B.

Quote: In some programming languages iota is used to represent an array of consecutive integers.

Iota: Wikipedia
Golang program that uses iota in const package main import "fmt" const ( Low = 5 * iota Medium High ) func main() { // Use our iota constants. fmt.Println(Low) fmt.Println(Medium) fmt.Println(High) } Output 0 5 10
Cannot assign const. A constant name cannot be assigned to another value. It is not a variable. The Go compiler will report an error here. With var, the program will compile.
Golang program that causes assignment error package main const ( Normal = 1 Medium = 2 ) func main() { Medium = 3 } Output C:\programs\file.go:13: cannot assign to Medium
One const syntax. A constant can be declared in a single-line statement. We can also specify constants within func bodies. Here we create a constant string of value "Blue."
Golang program that uses constant in func package main import "fmt" func main() { // Use a constant string in a func. const color = "Blue" // Test the constant. if color == "Blue" { fmt.Println(color) } } Output Blue
Multiple consts syntax. We can declare multiple consts on one line. No parentheses are required. Here we have a const string and a const untyped integer.

Note: By default, a const integer value is untyped. A type like int64 can be specified.

Golang program that uses multiple consts on one line package main import "fmt" func main() { // Declare multiple consts on one line. const name, size = "carrot", 100 // Display values. fmt.Println(name) fmt.Println(size) } Output carrot 100
Type int16. A const can be typed. Here we specify the type int16 for the const "v." We add the int16 to a slice of int16 elements.

Tip: By default, integer consts are untyped integers. These can be used as int16 or other integral values.

Golang program that uses const with int16 type package main import "fmt" func main() { // A const can have an explicit type. const v int16 = 10 // Create a slice with the constant int16. slice := []int16{v, 10} fmt.Println(slice) } Output [10 10]
Type error. Sometimes using a type on an integer constant will cause an error. The type must be correct for the rest of the program. Otherwise the program will not compile.
Golang program that causes error with typed const package main import "fmt" func main() { const v int32 = 1000 // The const int32 cannot be used in an int16 slice. slice := []int16{v, 10} fmt.Println(slice) } Output cannot use v (type int32) as type int16 in array element
Overflow. With const typed integers, overflow is detected at compile-time. Here we try to use an invalid number in an int16. The program never reaches execution.
Golang program that uses invalid const int16 package main import "fmt" func main() { // We cannot have an int16 with this value. const v int16 = 1000000 // Not reached. fmt.Println(v) } Output constant 1000000 overflows int16
Var, variables. We can declare variables in a var block. Here variables are initialized at runtime, and they can be reassigned in methods. They can be used throughout the program.
Golang program that uses var package main import ( "fmt" "time" ) var ( Month = time.Now().Month() Year = time.Now().Year() ) func main() { // Use variables declared in var. fmt.Println(Month) fmt.Println(Year) } Output January 2015
Var, short syntax. In Go we can use the var keyword with a type or without a type. We can declare variables with var. But we can also use a short declaration syntax form.

Version 1: This program uses a var declaration with no type specified. The string "animal" is assigned to different values and printed.

Version 2: This version uses a short variable declaration with the ":=" characters. It does the same thing as program version 1.

Golang program that uses variable declaration, var keyword package main import "fmt" func main() { // Use a local variable string. var animal = "Cat" fmt.Println(animal) // Reassign the local variable. animal = "Dog" fmt.Println(animal) } Output Cat Dog Golang program that uses short variable declarations package main import "fmt" func main() { // Use short variable declaration. animal := "Cat" fmt.Println(animal) // Reassign it. animal = "Dog" fmt.Println(animal) } Output Cat Dog
Typed vars. A var declaration can have more than one variable in it, and each can have a separate value. But only one type can be used. All variables use the specified type.

Quote: If a type is present, each variable is given that type (Go Specification).

Golang program that uses two typed vars, one line package main import "fmt" func main() { // Create two vars of type int32 on one line. var n, y int32 = 100, 200 // Display variables. fmt.Println(n) fmt.Println(y) } Output 100 200
With const and iota, Go provides compile-time code generation features. We can specify many constants with just an expression. With var we separate variables into a block.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls