Package feature implements a simple abstraction for feature flags with arbitrary values.
A flag is registered on a FlagSet.
Flags are created using a specific method based on the type of the value of the flag, named after the type.
Currently, the supported methods are
- FlagSet.Any and FlagSet.AnyFunc for flags with arbitrary values,
- FlagSet.Bool and FlagSet.BoolFunc for boolean flags,
- FlagSet.Duration and FlagSet.DurationFunc for duration flags,
- FlagSet.Float64 and FlagSet.Float64Func for float flags,
- FlagSet.Int and FlagSet.IntFunc for int flags,
- FlagSet.String and FlagSet.StringFunc for string flags,
- FlagSet.Uint and FlagSet.UintFunc for uint flags.
Each method will return a callback that takes a context.Context and returns a value of the specific type.
For example:
package main
import (
"context"
"github.com/nussjustin/feature"
)
func main() {
var set feature.FlagSet
myFeature := set.Bool("my-feature", "some new feature", false)
if myFeature(context.Background()) {
println("my-feature enabled") // never runs, see next section
}
}It is also possible to register a flag with a callback that is used to determine the flag value dynamically:
package main
import (
"context"
"github.com/nussjustin/feature"
)
func main() {
var set feature.FlagSet
myFeature := set.BoolFunc("my-feature", "some new feature", func(ctx context.Context) bool {
// ... do something with ctx ...
return false
})
if myFeature(context.Background()) {
println("my-feature enabled") // never runs, see next section
}
}By default, the values returned for each flag will be the default value specified when creating the flag.
The FlagSet.Context method can be used to set custom values for feature flags on a per-context basis.
Example:
package main
import (
"context"
"github.com/nussjustin/feature"
)
func main() {
var set feature.FlagSet
myFeature := set.Bool("my-feature", "some new feature", false)
// Enable the feature for our context
ctx := set.Context(context.Background(),
feature.BoolValue("my-feature", true))
if myFeature(ctx) {
println("my-feature enabled")
}
}Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.