dpq755012465
2019-01-23 00:57
浏览 146
已采纳

在Go中使用位掩码(使用iota进行枚举)

Bit stuck.

I'm trying to work out how to get the bitmask values from a const with go enumeration. For example, if key is 5, which is 0101 bits, it would be dog and fish. What is the easiest way to get the bit values (1,2,4,8,16, 32, 64 etc) so I can select the string values and return the set of animals?

type Key int

const (
    Dog Key = 1 << iota
    Cat
    Fish
    Horse
    Snake
    Rabbit
    Lion
    Rino
    Hedgehog
)

Been reading but I can't work this out.

1条回答 默认 最新

相关推荐 更多相似问题