This is a creation in Article, where the information may have evolved or changed.
4.3 Constants
Constants are defined using the keyword const, which is used to store data that does not change.
The data types stored in constants can only be Boolean, numeric (integer, float, and plural) and string.
The definition format of a constant: const identifier [type] = value
for example:
const Pi = 3.14159
In the Go language, you can omit the type specifier [type]
, because the compiler can infer its type based on the value of the variable.
- An explicit type definition:
const b string = "abc"
- An implicit type definition:
const b = "abc"
When a constant without a specified type is used, it infers the type it needs based on its use of the environment. In other words, constants of undefined types get the correlation type based on the context at the necessary moment.
var n intf(n + 5) // 无类型的数字型常量 “5” 它的类型在这里变成了 int
The value of a constant must be able to be determined at compile time, and you can involve the evaluation process in its assignment expression, but all the values used for the calculation must be available during compilation.
- The right approach:
const c1 = 2/3
- Wrong approach:
const c2 = getNumber()
//Raises build Error:getNumber() used as value
Because custom functions are unknown during compilation, they cannot be used for constant assignment, but built-in functions can be used, such as Len ().
A numeric constant is no size and symbol, and can use any precision without causing an overflow:
const Ln2= 0.693147180559945309417232121458\176568075500134360255254120680009const Log2E= 1/Ln2 // this is a precise reciprocalconst Billion = 1e9 // float constantconst hardEight = (1 << 100) >> 97
As we can see from the above example, backslashes \
can be used as multiple-line connectors in a constant expression.
You don't have to worry about type conversion between constants, as they are very good numbers, compared to various types of numeric variables.
It is important to note, however, that when a constant is assigned to a numeric variable of too small a precision, it may cause an overflow because the numeric value represented by the constant cannot be correctly expressed, which throws an error during compilation. In addition, constants allow the use of parallel assignments in the form of :
const beef, two, c = “meat”, 2, “veg”const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = 1, 2, 3, 4, 5, 6const (Monday, Tuesday, Wednesday = 1, 2, 3Thursday, Friday, Saturday = 4, 5, 6)
Constants can also be used as enumerations:
const (Unknown = 0Female = 1Male = 2)
The first is iota
equal to 0, and whenever a iota
new line is used, its value is automatically incremented by 1, so it a=0, b=1, c=2
can be shortened to the following form:
const (a = iotabc)
( Translator Note: The use of iota involves very complex and diverse situations, and the author's explanation is not clear, because it is difficult to make an intuitive text description of Iota's usage.) To learn more, watch the video tutorial "Go Programming Basics", lesson Fourth: constants and operators
iota
It can also be used in expressions, such as: iota + 50
. Each time a new constant block or a single constant declaration is encountered, iota
it resets to 0 ( simply speaking, Iota resets to 0 for each time the Const keyword is encountered ).
Of course, a constant is a constant constant, so we cannot modify its value while the program is running, and if you attempt to modify the value of a constant in your code, a compilation error is thrown.
Reference a time
piece of code in a package as an example: the name of the day of the week.
const (Sunday = iotaMondayTuesdayWednesdayThursdayFridaySaturday)
You can also use a type as the type of the enumeration constant:
type Color intconst (RED Color = iota // 0ORANGE // 1YELLOW // 2GREEN // ..BLUEINDIGOVIOLET // 6)