⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Generic and iterable bit flags in Go

License

Notifications You must be signed in to change notification settings

coxley/bitflags

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bitflags

Go Reference

This covers a specific use-case for bit flags where:

  • You want the flags themselves defined separately.
  • You want to iterate over set values, and rely on exhaustive-switch linters to make you account for everything.
  • You want generic support for "safety" (convenience), and support any type with constraints.Unsigned storage.

A code block is worth 1000 words:

package main

import (
	"fmt"

	"github.com/coxley/bitflags"
)

type perms uint8

const (
	read perms = 1 << iota
	write
	exec
)

func main() {
	var flags bitflags.Set[perms]
	flags.Add(read, exec)    // alt: bitflags.NewSet(read, exec)
	flags.HasAll(read, exec) // true
	flags.Has(write)         // false

	// Output:
	//   read: set
	//   exec: set
	for flag := range flags.All() {
		switch flag {
		case read:
			fmt.Println("read: set")
		case write:
			fmt.Println("write: set")
		case exec:
			fmt.Println("exec: set")
		}
	}
}

About

Generic and iterable bit flags in Go

Topics

Resources

License

Stars

Watchers

Forks

Languages