### 1.1 Constant Declaration

A constant is an identifier of a simple value that is not modified when the program is running.

``Const identifier [type] = value const b string = "ABC" const b string = "ABC"  const b = "abc" package main import "fmt" func main() { const LENGTH int = 10 const WIDTH int = 5 var area int const a, B, c = 1, false, "STR"; b, c = 1, false, "STR";Copy the code``

Running result:

``Area: 50 1 false STRCopy the code``

Constants can be used as enumerations, groups of constants

``````const (
Unknown = 0
Female = 1
Male = 2
)
Copy the code``````

If the type and initialization value are not specified in the constant group, it is the same as the non-empty constant Rvalue in the previous line

``````package main

import (
"fmt"
)

func main() {
const (
x uint16 = 16
y
s = "abc"
z
)
fmt.Printf("%T,%v\n", y, y)
fmt.Printf("%T,%v\n", z, z)
}
Copy the code``````

Running result:

``Uint16, 16 string, ABCCopy the code``

Constant note:

• The data types in constants can only be Boolean, numeric (integer, floating point, and complex), or string
• Constants that are not used will not report errors at compile time
• When displaying the specified type, you must ensure that the left and right values of the constant are of the same type, and use a display type conversion if necessary. This is different from variables, which can have different types of values

### 1.2 iota

Iota, special constant, can be thought of as a constant that can be modified by the compiler

Iota can be used as an enumeration value:

``````const (
a = iota
b = iota
c = iota
)
Copy the code``````

The first iota is equal to 0, and its value is automatically incretracted by 1 every time iota is used on a new line; So a=0, b=1, c=2 can be abbreviated as:

``````const (
a = iota
b
c
)
Copy the code``````

### Iota usage

``Package main import "FMT" func main() {const (a = iota //0 b //1 c //2 d = "ha", Iota +=1 e //"ha" iota +=1 f //"ha" iota +=1 g //100 iota +=1 h //7, restore the count I //8) fmt.Println(a,b,c,d,e,f,g,h,i) }Copy the code``

Running result:

``````0 1 2 ha ha 100 100 7 8
Copy the code``````

If the interrupt IOTA increments, an explicit restore must be performed. And the subsequent increment is incremented in row order

Increment is int by default and can display the specified type

Numeric constants do not allocate storage space and do not need to be addressed in memory like variables, so addresses cannot be obtained