Skip to content

Simple feature flag package for Go

License

Notifications You must be signed in to change notification settings

nussjustin/feature

Repository files navigation

feature Go Reference Lint Test

Package feature implements a simple abstraction for feature flags with arbitrary values.

Examples

Registering a flag

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

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
	}
}

Context-specific values

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")
	}
}

Contributing

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.

License

MIT

About

Simple feature flag package for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages