This is a creation in Article, where the information may have evolved or changed.
The following excerpt from the Go programming Language:
A Const declaration gives names to constants, which is, the values that were fixed at compile time. The value of a constant must be a number, string, or Boolean.
constCan only be a number, a string, or a Boolean value.
A constant declaration may specify a type as well as a value, if the absence of an explicit type, the type is inferred From the expression on the right-hand side.
When you define a constant without specifying its type, the type of the constant is determined by the expression to the right of the equal sign.
Here's how to define a group constant :
When a sequence of constants are declared as a group, the right-hand side expression may be omitted for all but the first O f the group, implying the previous expression and its type should be used again. For example:
Const (
A = 1
B
c = 2
D
)
Fmt. Println (A, B, C, D)//"1 1 2 2"
A const declaration may use the constant generator iota, which are used to create a sequence of related values without Spel Ling out each one explicitly. In a const-declaration, the value of iota begins at zero and increments by one for each item in the sequence. For example:
Type Weekday int
Const (
Sunday Weekday = Iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
This declares Sunday to is 0, Monday to is 1, and so on.
In addition, about untyped constants the definition:
Constants in Go is a bit unusual. Although a constant can has any of the basic data types like int or float64, including named basic types like time. Duration, many constants is not committed to a particular type. The compiler represents these uncommitted constants with much greater numeric precision than values of basic types, and AR Ithmetic on them are more precise than machine arithmetic; Assume at least for precision. There is six flavors of these uncommitted constants, called untyped Boolean, untyped integer, untyped rune, untyped float Ing-point, untyped complex, and untyped string.
Only constant if it is possible untyped , the variable cannot be untyped .
Only constants can is untyped. When an untyped constant are assigned to a variable, as in the first statement below, or appears on the right-hand side of A variable declaration with a explicit type, as in the other three statements, the constant is implicitly converted to th E type of that variable if possible.
var f float64 = 3 + 0i//untyped complex-float64
F = 2//untyped integer--float64
f = 1e123//untyped floating-point-float64
f = ' a '//untyped Rune-Float64
The statements above is thus equivalent to these:
var f float64 = float64 (3 + 0i)
f = float64 (2)
f = float64 (1e123)
f = float64 (' a ')
constantBetween conversions to consider whether it is possible to overflow:
Whether implicit or explicit, converting a constant from one type to another requires that the target type can represent t He original value. Rounding is allowed for real and complex floating-point numbers:
Const (
Deadbeef = 0xdeadbeef//untyped int with value 3735928559
A = UInt32 (Deadbeef)//UInt32 with value 3735928559
b = float32 (Deadbeef)//float32 with value 3735928576 (rounded up)
c = float64 (Deadbeef)//Float64 with value 3735928559 (exact)
D = Int32 (Deadbeef)//Compile Error:constant overflows int32
E = float64 (1e309)//compile error:constant overflows float64
f = UINT ( -1)//compile error:constant underflows uint)
Be aware untyped constant of the choice of variable type when assigning to a variable.
In a variable declaration without an explicit type (including short variable declarations), the flavor of the untyped cons Tant implicitly determines the default type of the variable, as in these examples:
I: = 0//untyped integer; implicit int (0)
r: = ' \000 '//untyped rune; Implicit rune (' \000 ')
F: = 0.0//untyped floating-point; implicit float64 (0.0)
c: = 0i//untyped complex; Implicit complex128 (0i)
Note the asymmetry:untyped integers is converted to int, whose size was not guaranteed, but untyped floating-point and co Mplex numbers is converted to the explicitly sized types Float64 and complex128. The language have no unsized float and complex types analogous to unsized int, because it's very difficult to write Correc T numerical algorithms without knowing the size of one ' s floating-point data types.
To give the variable a different type, we must explicitly convert the untyped constant to the desired type or the de Sired type in the variable declaration, as in these examples:
var i = int8 (0)
var i int8 = 0
These defaults is particularly important when converting a untyped constant to an interface value since they determine I TS dynamic type.
Fmt. Printf ("%t\n", 0)//"int"
Fmt. Printf ("%t\n", 0.0)//"Float64"
Fmt. Printf ("%t\n", 0i)//"complex128"
Fmt. Printf ("%t\n", ' \000 ')//"Int32" (Rune)