Go language Learning-constants

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The following sentence is from "Go Language Programming":

In the go language, constants are untyped. As long as the constant is within the range of the corresponding type, it can be used as a constant of that type, such as constant-12, which can be assigned to variables of type int, Int32, Int64, float32, float64, complex64, complex128, and so on. ”

Note: Personal understanding, here "no type", personal understanding is not fixed type, not really no type.

1, constant definition: const

Constant definitions in the go language are defined directly with the Const keyword, just as in C + +.

The difference is that when you define a constant with const in go, the subsequent constant type is not required, and the personal feeling can be understood in 2 ways:

(1). As described at the beginning of this document, the Go language constants are invariant types, and the compiler itself determines the type based on the value to the right of the equals sign;

(2). It can also be understood that the keyword is similar to Var, which can be used when defining variables (constants) at the same time as a keyword, without the need for a type.

A constant definition must be a value that can be determined at compile time, such as:

Const TMPCONST 1 << 3

Since 1 << 3 can determine the value at compile time, there is no problem with this line of command, the following command compiles an error:

Const HOME = os. GETENV ("HOME")

This command needs to be evaluated after running getenv, so this command compiles with an error.

2. Pre-defined constants

Predefined constants in the Go language are: Ture, false, and iota.

Ture and false are easier to understand, respectively, for logical true and logical false.

Iota This constant is very special, iota each encounter const, will be set to 0, if encountered is not const, then each occurrence of the value of the Iota,iota increase by 1. Such as:

Const (//encountered Const,iota is assigned to 0

C0 = iota//At this time, Tota = = 0, so C0 = = 0, while iota++, so this assignment ends iota== 1

C1 = Iota//c1 = = 1

C2 = Iota//c2 = = 2

)

If the expression of the two Const assignment statement is the same, the subsequent assignment expression can be omitted, and the compiler defaults to the same expression as the preceding one. Such as:

Const

(

C0 = Iota

C1//equivalent to C1 = Iota

C2//equivalent to C2 = Iota

)

Const

(

C0= 3

C1//equivalent to C1 = 3

C2//equivalent to C2 = 3

)

3. Enumeration

The enum keyword is not provided in the go language to define enumeration types, and the ability to implement enumerations can be implemented using the const +iota in the previous section. Such as:

Const

(

sunday=Iota

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

numberofdays// This constant is not exported

)

Note: Go in the language, the symbol starting with the uppercase letter is " Public "The, can be seen outside of the bag, so above the numberofdays can only be visible within this package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.