The constant generator iota golang

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. In a const declaration statement, in the first line of the constant declaration where, the IOTA will be set to 0 , then add a line in each of a constant declaration.

 

The following are examples of the package from the time it first defines a Weekday named type, and defines a constant for the day of the week, Sunday from 0 starts. In other programming languages, this type generally referred to as an enumerated type.

 

type Weekday int
const (
  Sunday Weekday = iota
  Monday
  Tuesday
  Wednesday
  Thursday
  Friday
  Saturday
)

 

Sunday will correspond to 0, Monday is 1, and so on.

 

We can also use the complex constant expressions iota, following from net example package for a minimum of an unsigned integer 5bit each bit specify a name:

 

type Flags uint
const (
    FlagUp Flags = 1 << iota // is up
    FlagBroadcast // supports broadcast access capability
    FlagLoopback // is a loopback interface
    FlagPointToPoint // belongs to a point-to-point link
    FlagMulticast // supports multicast access capability
)

 

With increasing iota, each corresponding to a constant expression . 1 << iota , continuous two powers, one for each bit position. Using these constants can be used for testing, set or clear the corresponding bit value of the bit

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiangxiaolin/p/11886441.html