You can use a bitmask. But the bitmask approach is more effective if you set up your flags to be a bitmask to begin with, i.e. the flags should be integers that can be OR'ed together. bool in Go doesn't support | operator.
With a bitmask, to check if more than one bit is set you can use the trick x & (x - 1) != 0. This checks if x is a power of two, when it is not, more than one bit is set. (source)
type alg int const ( md5flag alg = 1 << iota sha1flag sha256flag sha512flag ) func main() { // clients can initialize the value with bitwise-OR supportedAlgos := md5flag | sha256flag // check if more than one is set if supportedAlgos & (supportedAlgos - 1) != 0 { // do stuff } }
If you can't refactor your code to use a bitmask you may still construct it from the individual bool pointers. But then the approach isn't much different than the one you have right now.
func toBitmask(bs ...*bool) int { bitmask := 0 for i, b := range bs { if b != nil && *b { bitmask |= 1 << i } } return bitmask } func main() { list := toBitmask(md5flag, sha1flag, sha256flag, sha512flag) if list & (list - 1) != 0 { // do stuff } }
Playground: https://play.golang.org/p/PXK_1sS5ZxI
[md5flag, sha1flag, sha256flag, sha512flag].sum { |f| f ? : 1: 0 }would be perfectif *md5flag || *sha1flag || *sha256flag { ... }.