2021-12-15 04:49:03 -05:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-02-01 15:36:56 -05:00
|
|
|
const OPTNONE bitmask.MaskBit = 0
|
2021-12-15 04:49:03 -05:00
|
|
|
const (
|
2022-02-01 15:36:56 -05:00
|
|
|
OPT1 bitmask.MaskBit = 1 << iota
|
2021-12-15 04:49:03 -05:00
|
|
|
OPT2
|
|
|
|
OPT3
|
|
|
|
// ...
|
|
|
|
)
|
|
|
|
|
2022-02-01 15:36:56 -05:00
|
|
|
var MyMask *bitmask.MaskBit
|
2021-12-15 04:49:03 -05:00
|
|
|
|
|
|
|
func main() {
|
2022-02-01 15:36:56 -05:00
|
|
|
MyMask = bitmask.NewMaskBit()
|
2021-12-15 04:49:03 -05:00
|
|
|
|
|
|
|
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)
|
2022-02-01 15:36:56 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-12-15 04:49:03 -05:00
|
|
|
*/
|
|
|
|
package bitmask
|