This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
constant
A constant is an expression that ensures that the value of an expression is evaluated at compile time, rather than at run time, so that the compiler knows its value. All constants are essentially of their basic type: Boolean, string, or number
The declaration of a constant defines a named value that looks syntactically like a variable, but the value is constant, which prevents accidental (or malicious) modifications during the program’s run. For example, to represent mathematical constants such as PI, constants are better than variables in Go programs because their values are constant
Const PI = 3.14159 // Approximate; Math.pi is a more accurate approximationCopy the code
Similar to variables, the same declaration can define a list of constants that apply to a set of related values:
PI = const (e = 2.71828182845904523536028747135266249775724709369995957496696763 3.14159265358979323846264338327950288419716939937510582097494459)Copy the code
Many calculations for constants can be done at compile time to reduce runtime effort. Some errors are usually detected at run time, but are reported at compile time if the operands are constant, such as integer divided by 0, string subscripts out of bounds, and any floating-point operation that produces an infinite value
Because a constant’s value is known at compile time, it can appear in declarations involving types, such as the length of an array type
Const IPv4Len = 4 // parseIPv4 function to explain an IPv4 address (d · d.d · d) func parseIPv4(s string) IP {var p [IPv4Len]byte //... }Copy the code
Constant declarations can specify both a type and a value, and if the type is not explicitly specified, the type is inferred from the expression on the right. Duration is a named type whose basic type is int64, and time.Minute is also a constant based on int64. The two constants declared below are of type time.duration, shown as % T (click here if you don’t remember the meaning of the placeholder for the output format)
const noDelay time.Duration = 0
const timeout = 5 * time.Minute
fmt.Printf("%T %[1]v\n", noDelay) // time.Duration 0
fmt.Printf("%T %[1]v\n", timeout) // time.Duration 5m0s
fmt.Printf("%T %[1]v\n", time.Minute) // time.Duration 1m0s
Copy the code
If you declare a set of constants at the same time, all expressions to the right of the equal sign except for the first term can be omitted, which means that the expression and its type of the preceding term are reused. For example,
const (
a = 1
b
c = 2
d
)
fmt.PrintlnCa, b, c, d) // 1 1 2 2
Copy the code
This is not very practical if the reuse of the right-hand side expression results in always the same result. What if the result is variable? This is where the constant generator IOTA comes in
Constant generator IOTA
Constants can be declared using the constant generator IOTA, which creates a list of related values rather than explicitly writing each value out. In constant declarations, ioTA starts at 0 and increments by 1
type weekday int
const (
Sunday weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
Copy the code
In the above statement, Sunday = 0, Monday = 1, and so on
And then let’s do a slightly more complicated one
Const (_ = 1 << (10*iota) KiB // 1048576 GiB // 1073741824 TiB // 1099511627776 (more than 1 << 32) PiB //... EiB // ... ZiB // ... (more than 1 << 64) YiB //...)Copy the code
Iota has limitations, such as the inability to generate the more familiar powers of 1000 because there is no exponent operator
Untyped constant
Go’s constants are special in their own way. Although constants can be of any primitive data type, such as int or Float64, as well as named primitive types (such as time.duration), many constants are not dependent on a specific type. The compiler represents these dependent types of undetermined constants as values that are more numerically accurate than the basic types and more arithmically accurate than the native machine precision. They can be considered accurate to at least 256 bits. There are six types of constants whose dependent types are untyped Boolean, untyped integer, untyped literal symbol, untyped floating point number, untyped complex number, and untyped string
Note: Only constants can be untyped. If a typeless constant is declared as a variable or appears to the right of an assignment to an explicitly typed variable, it is implicitly converted to the type of the variable
By delaying the determination of dependent types, untyped constants not only temporarily maintain higher precision, but they can also be written into more expressions than type-determined constants without casting the type. For example, the values ZiB and YiB in the above example are too large to be stored with any type of integer, but they are both legal constants and can be used in the following expression
fmt.Println(YiB/ZiB) // "1024"
Copy the code
For example, the floating-point constant math.pi can be used anywhere a floating-point value or complex number is required
var x float32 = math.Pi
var y float64 = math.Pi
var z complexl28 = math.Pi
Copy the code
If the constant Math.pi is initially determined to be of a specific type, such as Float64, the result will be less accurate. Alternatively, if you eventually need a float32 value or a ComplexL28 value, you may need to convert the type
const Pi64 float64 = math.Pi
var x float32 = float32(Pi64)
var y float64 = Pi64
var z complexl28 = complexl28(Pi64)
Copy the code
Note the asymmetries of the types: untyped integers can be converted to ints of uncertain size, but untyped floating-point numbers and untyped complex numbers are converted to float64 and Complexl28 of explicit size. In the Go language, there are only unspecified int types, but there are no unspecified float and complex types, because it is difficult to write the correct numerical algorithm if the size of floating point data is unknown
To convert a variable to a different type, we must either explicitly convert an untyped constant to the desired type, or specify the desired type when we declare the variable, as shown in the following example
var i = int8(0)
var i int8 = 0
Copy the code
reference
Go Programming Language — Alan A. A. Donovan
Go Language Learning Notes — Rain Marks