[Golang] grammar basis of constant

Among the go language, it does not change the data is called constants, keyword constto declare.

Constants are usually created at compile time, even constants defined in a function, too, at the same time constant only type boolean, numeric (integer, floating point and complex) and string.

Due to limitations compile time, the definition of a constant expression must evaluate to be a constant value of the expression compiler.

Constant declaration

Constant definition syntax is as follows:

const name type = value 

For example, we define a constant following:

const mysql_password string = "abc123"

Of course, the definition of the constants and variables may be the same, omitted typetypes. As the compiler to compile variable inference as to the type of data.

E.g:

const mysql_user = "root"

If you need to declare a number of constants, variables can also be declared in a manner similar can be used.

const (
    IP  = "127.0.0.1"
    pi = 3.1415926
)

If the time constant declaration batches, except a first constant value to be set, the other behind the constant value can be omitted, the same values ​​will be omitted and the first constant value is a constant.

const (
    a = 1
    b
    c
    d
)

In the above procedure, we declare four constants, a, b, c, d, four values ​​of the constants are equal to 1.

Because the value of the constant is determined during compilation, thus it may form part of a constant type, such as the type specified for the length of the array:

const IPv4Len = 4
// parseIPv4 解析一个 IPv4 地址 (d.d.d.d).
func parseIPv4(s string) IP {
    var p [IPv4Len]byte
    // ...
}

Constant generator

Constant declaration can be used to initialize iota constant generator, which is used to generate a set of constants to initialize a similar rule, but do not write it again each line initialization expression. Const in a declaration statement, in the first line of the constant declaration where, iota will be set to 0, then add a line in each of a constant declaration.

For example the following example:

const (
    a = iota 
    b
    c
    d
)

The above values of four constants are by iotainitializing constants for the generator, a is 0, the value of constant following three values are respectively incremented by 1. 2,3.

Untyped constants

Among the go language, you can either specify the type when declaring the constant to constant, you can not specify the type.

In which the compiler provides a higher precision than the arithmetic operation of basic fundamental data types not explicitly numeric constant, it is considered that at least the operation precision 256bit.

Untyped and typed numeric constants

You can either specify the type when declaring constants in the Go language, you can not specify the type. When we declare a literal in the code, we in fact declare an anonymous type of constant is not specified.

The following example shows untyped, named constants and typed constants Anonymous:

const a = 12345
const b = 3.14159

const c int = 12345 
const d float64 = 3.14159

Statement on the left is named constants, and the right of the literal fact is anonymous constant.

The type of numeric constants

Need to know is, the type of system variables and constants type system just looks the same, not the same. How constants represent the values ​​associated with them, there is another way to achieve the set.

When we declare a type specified time constant, is specified when the type declaration for defining the accuracy of the constant, and it does not change the value of the constant in the underlying data structure, implemented as a variable with what the internal data structure value, different compilers have different implementations. So we think is best, constants have is the type (kind), rather than the type (type)

A numeric constants can be one of the following types: integers, floating point numbers, complex numbers, Unicode characters (rune):

12345    // kind: 整型
3.141592 // kind: 浮点数
1E6      // kind: 浮点数

In the example above, we declare three numeric constants, one belonging to the integer types, the latter two are part of the floating-point types. Literal form it will determine what kind of constant. When it does not include the decimal point or exponential form, and this is a constant integer types.

Example perfectly accurate constant

We can declare an int value exceeds the maximum range of constants, to illustrate the accuracy of the constant is very large.

package main

import "fmt"

// 远远大于 int64
const myConst = 9223372036854775808543522345

func main() {
    fmt.Println("Will Compile")
}

The above program can compile because the integer type constant completely accurate.

If we take the above into a constant specified int64type, it means that this range has been limited to a constant int64within the range, and this time the program will not compile successfully:

package main

import "fmt"

// Much larger value than int64.
const myConst int64 = 9223372036854775808543522345

func main() {
    fmt.Println("Will NOT Compile")
}

Compiler Error:
./ideal.go:6: constant 9223372036854775808543522345 overflows int64

From the above we can know, kind of integer constants can represent very large numbers. And you can understand why we say that the constant is completely accurate.

Numeric constant declaration

When we declare an untyped constant time, Go does not have any requirements on the values ​​of the constants:

const a = 12345 // 种类:整数
const b = 3.14159 // 种类:浮点数

The example above, the constant and the left is given a constant value and the right side of the same species.

When we declare there is a type of constant time, constant form of the right must be compatible with the type of the constant declarations of the left:

const a int           = 12345    // 种类:整数
const b float64 = 3.141592 // 种类:浮点数

The value of the right side of the statement must also be within the effective range of the type of declaration. For example, the following statement numeric constants are not valid:

const myUint8 uint8 = 1000

uint8Can only represent numbers range from 0 to 255, which is we said above, the specified type is used to define the constant accuracy.

Implicit conversion of plastic

Go in the world, the variables are not implicit conversion takes place. However, implicit type conversions between the constant and variable often occur.

Let's take a look at the implicit conversion of plastic:

var myInt int = 123

In this example, we have a type integer constant 123, which is implicitly converted into inttype. Because of this constant literal or index is not a decimal point, this will serve as a constant integer types of constants. Type integer constants can be converted implicitly to a signed or unsigned variables any length, as long as the whole process is no data overflow occurs.

Constant floating type may also be converted implicitly to an integer variable, the value of the constant as long as the form is compatible integer:

var myInt int = 123.0

We can also be implicit constant - when the variable conversion, specify the type of omitted variables:

var myInt = 123

In this example, when we use the value of 123integer constant is initialized myIntwhen the variable myIntimplicitly using the default int64type.

Implicit conversion of floating-point type

We then take a look at the implicit floating-point conversion:

var myFloat float64 = 0.333

This time the compiler performs an implicit conversion from floating-point types of constant 0.333conversion to float64type float variable. Because of the constant literals containing a decimal point, so this type of floating-point constant is a constant. By default, a floating-point type of constants will be converted to float64a variable of type.

The compiler can also constant implicit type conversion to become an integer float64type variable.

var myFloat float64 = 1

In this example, the integer constants 1are implicitly converted to float64the type of the variable.

Guess you like

Origin www.cnblogs.com/liujunhang/p/12534434.html