Documentation ¶
Overview ¶
Package bitmask handles a flag-like opt/bitmask system.
See https://yourbasic.org/golang/bitmask-flag-set-clear/ for more information.
To use this, set constants like thus:
package main import ( "r00t2.io/goutils/bitmask" ) const OPTNONE bitmask.MaskBit = 0 const ( OPT1 bitmask.MaskBit = 1 << iota OPT2 OPT3 // ... ) var MyMask *bitmask.MaskBit func main() { MyMask = bitmask.NewMaskBit() MyMask.AddFlag(OPT1) MyMask.AddFlag(OPT3) _ = MyMask }
This would return true:
MyMask.HasFlag(OPT1)
As would this:
MyMask.HasFlag(OPT3)
But this would return false:
MyMask.HasFlag(OPT2)
If you need something with more flexibility (as always, at the cost of complexity), you may be interested in one of the following libraries:
. github.com/alvaroloes/enumer . github.com/abice/go-enum . github.com/jeffreyrichter/enum/enum
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MaskBit ¶
type MaskBit uint
MaskBit is a flag container.
func NewMaskBit ¶
func NewMaskBit() (m *MaskBit)
NewMaskBit is a convenience function. It will return a MaskBit with a (referenced) value of 0, so set your consts up accordingly.
It is highly recommended to set this default as a "None" flag (separate from your iotas!) as shown in the example.
func NewMaskBitExplicit ¶ added in v1.3.0
NewMaskBitExplicit is like NewMaskBit, but allows you to specify a non-zero (0x0) value.
func (*MaskBit) Bytes ¶ added in v1.3.1
Bytes returns the current value of a MasBit as a byte slice (big-endian).
If trim is false, b will (probably) be 4 bytes long if you're on a 32-bit size system, and b will (probably) be 8 bytes long if you're on a 64-bit size system. You can determine the size of the resulting slice via (math/)bits.UintSize / 8.
If trim is true, it will trim leading null bytes (if any). This will lead to an unpredictable byte slice length in b, but is most likely preferred for byte operations.
func (*MaskBit) ClearFlag ¶
ClearFlag removes MaskBit flag from m.
func (*MaskBit) HasFlag ¶
HasFlag is true if m has MaskBit flag set/enabled.
func (*MaskBit) ToggleFlag ¶
ToggleFlag switches MaskBit flag in m to its inverse; if true, it is now false and vice versa.