The constant generator iota

The constant generator iota is used for declaring a sequence of related values that uses incrementing numbers without the need to type each one of them explicitly.

Most of the concepts related to the const keyword, including the constant generator iota, will be illustrated in the constants.go file, which will be presented in four parts.

The first code segment of constants.go is as follows:

package main 
 
import ( 
    "fmt" 
) 
 
type Digit int 
type Power2 int 
 
const PI = 3.1415926 
 
const ( 
    C1 = "C1C1C1" 
    C2 = "C2C2C2" 
    C3 = "C3C3C3" 
) 

In this part, we declare two new types, named Digit and Power2, and four new constants, named PI, C1, C2 and C3.

A Go type is a way of defining a new named type that uses the same underlying type as an existing one. This is mainly used for differentiating between different types that might use the same kind of data.

The second part of constants.go is shown in the following Go code:

func main() { 
 
    const s1 = 123 
    var v1 float32 = s1 * 12 
    fmt.Println(v1) 
    fmt.Println(PI) 

In this part of the program, you define another constant (s1) that is used in an expression (v1).

The third part of the program is as follows:

    const ( 
        Zero Digit = iota 
        One 
        Two 
        Three 
        Four 
    ) 
    fmt.Println(One) 
    fmt.Println(Two) 

Here you see the definition of a constant generator iota based on Digit, which is equivalent to the following declaration of four constants:

    const ( 
        Zero = 0 
        One = 1 
        Two = 2 
        Three = 3 
        Four = 4 
    ) 

The last part of the program of constants.go is as follows:

    const ( 
        p2_0 Power2 = 1 << iota 
        _ 
        p2_2 
        _ 
        p2_4 
        _ 
        p2_6 
    ) 
 
    fmt.Println("2^0:", p2_0) 
    fmt.Println("2^2:", p2_2) 
    fmt.Println("2^4:", p2_4) 
    fmt.Println("2^6:", p2_6) 
 
} 

There is another constant generator iota here, which is a little different than the previous one. First, notice the use of the underscore character in a const block with a constant generator iota. This allows you to skip unwanted values. Second, the value of iota always increments, and it can be used in expressions, which is what occurred in this case.

Now let's see what really happens inside the const block. For p2_0, iota has the value of 0 and p2_0 is defined as 1. For p2_2, iota has the value of 2 and p2_2 is defined as the result of the expression 1 << 2, which is 00000100 in binary representation-the decimal value of 00000100 is 4, which is the result and the value of p2_2. Analogously, the value of p2_4 is 16, and the value of p2_6 is 32.

As you can see, the use of iota can save your time when it fits your needs!

Executing constants.go will generate the following output:

$ go run constants.go
1476
3.1415926
1
2
2^0: 1
2^2: 4
2^4: 16
2^6: 64  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.78.136