what is << iota in Go

tags: learning go programming

content

type RowLineFlags int
const (
	Revision RowLineFlags = 1 << iota
	Highlightable
	Elided
)
  • what does << iota do?
    • this is iota with bit shift operator << to create a set of flags with distinct bit patterns
    • 1 << iota means the flags only has one bit turned on
      • it’s a constant with a single bit turned on, like 0x01, 0x02, 0x04, etc
      • i.e.: in binary
Revision = 0001 (binary)
Highlightable = 0010 (binary)
Elided = 0100 (binary)
  • and in decimal
Revision = 1
Highlightable = 2
Elided = 4

up

down

reference