Golang constants const and iota

  • A const.

    • 1. Grammar
    • Example 2.
  • 2. Iota

    • 1. Knowledge
    • Example 2.
  • Iii. The combination of type can better express the practical meaning

A const.

A constant is an identifier of a simple value, an amount that will not be modified while the program is running.

In the Java programming specification, constants are usually all uppercase, but in Golang case has meaning, so not all constants are always all uppercase

In Golang, the uppercase letter means public and the lowercase letter means private

1. Grammar

constConstant name [data type] = valueCopy the code

The data type can be ignored, and the Golang compiler will automatically infer the data type.

Pay attention to the following points when using:

  1. The data types can only be Boolean, numeric (integer, floating point, and complex), and string
  2. Satisfies multiple assignments
  3. Constants are only defined and are not used
  4. Constants can be enumerated, groups of constants
  5. If the type and initialization value are not specified in a constant group, it is the same as the rvalue of a non-empty constant on the previous line
  6. When displaying the specified type, you must ensure that the left and right constant values are of the same type, and display type conversions can be performed if necessary. This is different from variables, which can be different types of values

Example 2.

1. Define individual constants

const monday int = 1 // Explicit type definition
const tuesday = "2"  // Implicit type definition
const wednesday = 3  // No errors are reported if no compilation is used
fmt.Printf("Monday has the data type: %T and the value: %d\n", monday, monday)
fmt.Printf("Tuesday datatypes: %T and values: %s\n", tuesday, tuesday)
Copy the code

The output is:

The data type for Monday is:intThe value is:1Tuesday's data type is:stringThe value is:2
Copy the code

2. Define a set of constants

There are no enumerated types in Golang, as there are Enum classes in Java or Python that implement enumerations, so Golang implements enumerations via const

const monday, tuesday, wednesday = 1.2.3
// The following definition is more recommended
const (
 one   = 1
 two   = 2
 three = 3
)
Copy the code

In a set of constants, if a constant has no initial value, it defaults to the previous line

const (
 one   = 1
 two   = 2
 three = 3
 four
 five
)
fmt.Printf("Four has the data type: %T and the value: %d\n", four, four)
fmt.Printf("Five has the data type: %T and the value: %d\n", five, five)
Copy the code

The output is:

The data type of four is:intThe value is:3The data type of five is:intThe value is:3
Copy the code

If I have a lot of constants, do I have to assign each one of them?

There is a keyword in Golang that solves this problem. When defining enumerations, it is usually used in conjunction with const

2. Iota

Iota, a special constant, can be thought of as a constant that can be modified by the compiler

1. Knowledge

When using ioTA, note the following: 1. Whenever a const is defined, the initial value of ioTA is 0. 2. Every time a constant is defined, it is automatically incremented by 1 3. If the IOTA increment is interrupted, it must be explicitly restored. The default value is int, and the specified type can be displayed automatically. 6. Iota can participate in calculation

The bold part is the one that needs to be paid attention to, and here is an example

Example 2.

1. Define const enumeration, starting with iota

const (
 one   = iota // The initial ioTA value is 0
 two   = iota / / from 1-6
 three = iota // increment by 1
)
fmt.Println("One has the value:", one)
fmt.Println("The value of two is:, two)
fmt.Println("Three has the value:", three)

Copy the code

Output:

The value of one is:0The value of two is:1The value of three is:2
Copy the code

And you can see that each term is incremented by 1. We do not need to write ioTA every time we define ioTA. Because of the const nature, ioTA increments except for the first term

2. If ioTA is not displayed, the constant will also increment

const (
 one   = iota // The initial ioTA value is 0
 two     
 three
 four
)
fmt.Println("One has the value:", one)
fmt.Println("The value of two is:, two)
fmt.Println("Three has the value:", three)
fmt.Println("The value of four is:, four)
Copy the code

Output:

The value of one is:0The value of two is:1The value of three is:2The value of four is:3
Copy the code

Can iota only define 0,1,2,3? What if I have another value that I want to use?

One very, very useful feature of IOTA in Golang is that IOTA can participate in calculations

3. Iota participates in the calculation

For example, define b, KB, MB… , etc.

const (
  b  = 1< < (iota * 10) // IoTA starts with 0, so 1 << (0 * 10)
  kb                    // 1 << (1 * 10)
  mb                    // 1 << (2 * 10)
  gb                    // 1 << (3 * 10)
  tb                    // 1 << (4 * 10)
  pb                    // 1 << (5 * 10)
 )
fmt.Println(b, kb, mb, gb, tb, pb)
Copy the code

Output:

1 1024 1048576 1073741824 1099511627776 1125899906842624
Copy the code

Is it very useful? Although there are no enumerated classes in Golang, enumerations can be easily implemented using const and IOTA

Iii. The combination of type can better express the practical meaning

In real development, enumerations usually make sense: months, VIP levels…

In the example above, we simply defined numeric constants. We can define a concrete type to represent these constants and implement a meaningful enumeration

type VipLevel int
const (
 vipOne VipLevel = 1 + iota
 vipTwo  
 vipThree 
 vipFour
 vipFive
)
fmt.Println(vipOne, vipTwo, vipThree, vipFour, vipFive)
Copy the code

Output:

1 2 3 4 5
Copy the code

To summarize, const and IOTA are often used together to define enumerations. To make the enumeration meaningful, the type type of type is customizable.

Pay more attention to the nature of const and IOTA. Be careful when using it. Practice makes perfect.

This article is formatted using MDNICE