SDK guide

Feature flags in Go with OpenFeature

How to add feature flags to a Go service: install the provider, evaluate flags in process without a network call, use them in HTTP handlers, and shut down cleanly.

Railflag's Go provider evaluates feature flags inside your process in under a microsecond, with no network call per check. It implements the OpenFeature Go SDK, so your code depends on the vendor-neutral API.

Install and connect

go get railflag.com/go/openfeature

You need Go 1.26 or newer. The only dependency is the OpenFeature Go SDK. Create a server key for the environment in the dashboard, and register the provider once in main:

import (
    "log"
    "os"

    "github.com/open-feature/go-sdk/openfeature"
    railflag "railflag.com/go/openfeature"
)

func main() {
    provider, err := railflag.NewProvider(railflag.Options{
        SDKKey:  os.Getenv("RAILFLAG_SERVER_KEY"),
        BaseURL: "https://relay.railflag.com",
    })
    if err != nil {
        log.Fatal(err)
    }
    if err := openfeature.SetProviderAndWait(provider); err != nil {
        log.Fatal(err)
    }
    defer openfeature.Shutdown()

    client := openfeature.NewDefaultClient()
    // pass client to your handlers
}

The provider downloads the flag config once, then follows changes over Server-Sent Events and picks them up within a second. If the stream drops it polls every 30 seconds, and if Railflag cannot be reached it keeps serving the last config. openfeature.Shutdown() closes the stream and sends any pending counts.

Evaluate a flag

Every check takes a context.Context, the flag key, a default value and an evaluation context with the targeting key and any attributes your rules use:

evalCtx := openfeature.NewEvaluationContext("user-42", map[string]any{
    "plan":    "pro",
    "country": "GR",
})

on, _ := client.BooleanValue(ctx, "new-checkout", false, evalCtx)
limit, _ := client.IntValue(ctx, "export-row-limit", 10000, evalCtx)

BooleanValue, StringValue, IntValue, FloatValue and ObjectValue, and their …ValueDetails variants, work as in any OpenFeature provider. An unknown flag or a wrong type returns your default together with an error, so a flag problem never has to become a failed request.

In an HTTP handler

Build the evaluation context from the request's user, and pass the request context through:

func checkout(client *openfeature.Client) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        user := userFrom(r)
        evalCtx := openfeature.NewEvaluationContext(user.ID, map[string]any{"plan": user.Plan})

        if on, _ := client.BooleanValue(r.Context(), "new-checkout", false, evalCtx); on {
            newCheckout(w, r)
            return
        }
        oldCheckout(w, r)
    }
}

Because a check allocates almost nothing and takes well under a microsecond, it is safe on a hot path. Context values can be Go numbers, strings, bools, slices and string-keyed maps, and time.Time values are compared as instants, so before and after rules work with them.

React to flag changes

fn := func(details openfeature.EventDetails) {
    log.Println("flags changed:", details.FlagChanges)
}
openfeature.AddHandler(openfeature.ProviderConfigChange, &fn)

The same answer in every language

evaluate.go is a port of Railflag's TypeScript evaluator and its tests replay the shared vectors, so a user lands in the same variation whichever SDK evaluates the flag. The known differences are small: regexes in matches rules run on Go's RE2, so a pattern with lookaround or backreferences matches nothing, and a date-time without a time zone is read as UTC.

Keys, privacy and experiments

  • Server keys can read every rule. Keep them on the server and out of mobile or browser code.
  • Once a minute the provider sends counts of how often each flag returned each variation, for Release Watch, from a background goroutine. It never sends contexts or user ids. Turn it off with DisableExposures: true.
  • For experiments, client.Track(ctx, "purchase", evalCtx, openfeature.NewTrackingEventDetails(49.9)) records conversions. Only events a running experiment counts are sent, and user keys are hashed before storage.
  • Server-side checks are never metered.

Frequently asked questions

How do I use feature flags in Go?

Run go get railflag.com/go/openfeature, create a provider with your server key, register it with openfeature.SetProviderAndWait, and call client.BooleanValue(ctx, key, default, evalCtx) wherever you branch. The provider holds the rules in memory and checks take under a microsecond.

Is the Go feature flag provider fast enough for hot paths?

Yes. Evaluation happens in your process, takes under a microsecond and makes no network call, so it is safe inside request handlers and loops.

What happens if Railflag is unreachable?

The provider keeps serving the last config it received. If the live stream drops it polls every 30 seconds until it recovers, and a flag check returns your default only if it never had a config.

Which Go version is required?

Go 1.26 or newer. The only dependency is the OpenFeature Go SDK.

Feature flags for your Go services

Local evaluation, live updates within a second, unlimited server-side checks on every plan.

Start for free