Golang enumeration Type-iota usage supplements

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

In high-level languages such as C #, Java, enumeration types are often used to represent States, and so on. There is no enumeration type in Golang, how do I implement enumerations? First, start with the concept of enumerations.

1. Enumeration type definition

From Baidu Encyclopedia query explained as follows: Http://baike.baidu.com/link?url= lrq57-yizj35grervdg2kh7lfgfxvvlcvsnbmaqjbuoqx3cjuynjp-9oz2-rf3jgmql3e4rbnsrqws_5nmai6k

Enumeration types are basic data types in some computer programming languages, such as C # or C++,JAVA,VB, rather than constructed data types, and are constructed in computer programming languages such as C. It is used to declare a set of named constants, which can be defined as enumerated types when there are several possible values for a variable.
An enumeration can create a new variable based on any of the data types in integer, Long, short, or byte. This variable can be set to one of the defined groups, effectively preventing the user from providing invalid values. This variable can make the code clearer because it can describe a specific value.

From the above know: is a set of named constants, the constant value can be continuous, or can be intermittent. The more formal understanding is as follows:

In the program design, sometimes used by a number of limited data elements of a collection, such as the week of the Monday to Sunday seven data elements of the collection, consisting of three colors red, yellow, green composition, a working team of 10 staff members of the collection and so on, the program in a variable value is limited to the elements in the collection. At this point, these data collections can be defined as enumerated types. Therefore, an enumeration type is a collection of possible values for a class of data, such as a collection of possible values for a week within a week:

{Sun,mon,tue,wed,thu,fri,sat}

The collection can be defined as an enumeration type that describes the day of the week, which has seven elements, so that an enumeration variable defined with an enumeration type can only take one of the element values in the collection. Because an enumeration type is an exported data type, you must first define the enumeration type, and then define the enumeration type variables with the enumeration types.

2. Enumeration type Considerations

Use the enumeration type to consider the following:

1. Enumeration concept: View 1, enum type definition

2. Enumeration member: Used to declare a new enumeration type. is a named constant for the enumeration type. Any two enumeration members cannot have the same name. Each enum member has an associated constant value. The type of this value is the underlying type of the enumeration. The constant value of each enumeration member must be within the range of the underlying type of the enumeration.

3. Enumeration member Default: The first enumeration member declared in an enumeration type it has an implied value of zero.

4. Enumeration members Display assignment: Allows multiple enumeration members to have the same value. The value of an enumeration member that does not display an assignment is always the value of the previous enumeration member +1.

5. Conversion of enum type to base type

3. Golang Enumeration Type Implementation

3.1 Enumeration Type implementation

Viewing 2.4, you can define enumeration types according to the Iota attribute:

The test output is as follows:

Enumeration type Use steps:

    • declaring enum types
    • Defining enumeration variables
    • Using enumeration variables

Note: From the Golang enumeration type definition, the variable enum can also be 99 other int types. From this perspective, Golang defines a wide range of enumeration types. Such as:

Advantages of 3.2 Iota

The implementation of Golang enumeration types from 3.1来 is also relatively simple and is programmed with the increment rules of iota.

Iota usage rules, see "Golang use Iota" for details.

3.3 How to use multiple iota

The values for the enumerated types defined in 3.1 Success=1, failed=2, duplicateevent=3, duplicatecommand=4, are based on the Iota increment rule, join to try Success=1, failed=2, Duplicateevent=0, duplicatecommand=1, is it possible? The answer is completely line-up, and the revision enumeration defines the format:

The test results are as follows:

3.4 Enumeration Type Definition Two

3.1 is an implementation, because Iota can also be used with an expression, so you can simplify the definition as follows:

The test is as follows:

The same as expected.

3.5 Summary

Use Iota to try the definition of the Golang enumeration type. However, the enumeration type of Golang is not a strictly enumerated type, so be aware of this.

Related Article

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.