const, iota Keywords
This page was last reviewed on Feb 19, 2023.
Dot Net Perls
Consts. In a Golang program, we can use a const block for a constant value. A const can never be reassigned. And its name will help us identify its meaning.
With iota we gain a way to generate groups of constants based on expressions. We can create an entire group of constants. This reduces possible issues with incorrect values.
Const, iota. Here we introduce the const keyword in a block of enumerated constants. We do not use all the features of const here. We declare 3 names and give them values.
Info Iota is an enumerator for const creation. The Go compiler starts iota at 0 and increments it by one for each following constant.
Warning 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.
package main import "fmt" const ( Cat = 10 Dog = 20 Bird = 30 ) const ( Low = 5 * iota Medium High ) func main() { // Use first const values. fmt.Println(Cat, Dog, Bird) // Use our iota constants. fmt.Println(Low, Medium, High) }
10 20 30 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.
package main const ( Normal = 1 Medium = 2 ) func main() { Medium = 3 }
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."
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) } }
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.
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) }
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.
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) }
[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.
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) }
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.
package main import "fmt" func main() { // We cannot have an int16 with this value. const v int16 = 1000000 // Not reached. fmt.Println(v) }
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.
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) }
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 code 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.
package main import "fmt" func Test1() { // Version 1: use a local variable string. var animal = "Cat" fmt.Println("TEST1: " + animal) // Reassign the local variable. animal = "Dog" fmt.Println("TEST1: " + animal) } func Test2() { // Version 2: use short variable declaration. animal := "Cat" fmt.Println("TEST2: " + animal) // Reassign it. animal = "Dog" fmt.Println("TEST2: " + animal) } func main() { Test1() Test2() }
TEST1: Cat TEST1: Dog TEST2: Cat TEST2: 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.
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) }
100 200
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 19, 2023 (edit).
© 2007-2024 Sam Allen.