4个iota转移示例


: , , . , , : , best practice , , . , , , , , . .


iota


  • iota 0, 1, 2,…
  • , const
  • const.

const (
    C0 = iota
    C1 = iota
    C2 = iota
)
fmt.Println(C0, C1, C2) // "0 1 2"

:


const (
    C0 = iota
    C1
    C2
)

, const — .



1 0, iota .


const (
    C1 = iota + 1
    C2
    C3
)
fmt.Println(C1, C2, C3) // "1 2 3"


, .


const (
    C1 = iota + 1
    _
    C3
    C4
)
fmt.Println(C1, C3, C4) // "1 3 4"

enum [best practice]


:


  • ,
  • iota,
  • String.

type Direction int

const (
    North Direction = iota
    East
    South
    West
)

func (d Direction) String() string {
    return [...]string{"North", "East", "South", "West"}[d]
}

:


var d Direction = North
fmt.Print(d)
switch d {
case North:
    fmt.Println(" goes up.")
case South:
    fmt.Println(" goes down.")
default:
    fmt.Println(" stays put.")
}
// Output: North goes up.


, caps . , NorthWest, NORTH_WEST.



iota — bitmask. ( “”), .


bitmasks .


:



All Articles