Go SDK
The RocketFlag Go SDK is a high-performance client for interacting with the RocketFlag API in your backend services.
Installation
Section titled “Installation”go get github.com/rocketflag/go-sdkBasic Usage
Section titled “Basic Usage”package main
import ( "fmt" "log" rocketflag "github.com/rocketflag/go-sdk")
func main() { // Initialize the client rf := rocketflag.NewClient()
// Fetch a flag flagKey := "your-flag-id" flag, err := rf.GetFlag(flagKey, rocketflag.UserContext{}) if err != nil { log.Fatalf("Error fetching flag: %v", err) }
if flag.Enabled { fmt.Printf("Feature '%s' is enabled!\n", flag.Name) }}Advanced Usage
Section titled “Advanced Usage”Working with Cohorts
Section titled “Working with Cohorts”Pass a UserContext with a “cohort” key:
userContext := rocketflag.UserContext{"cohort": "user@example.com"}flag, err := rf.GetFlag(flagKey, userContext)Working with Group Flags (Environments)
Section titled “Working with Group Flags (Environments)”Specify the environment in the UserContext:
userContext := rocketflag.UserContext{"env": "production"}flag, err := rf.GetFlag(flagKey, userContext)Protected Keys
Section titled “Protected Keys”Add the “key” to your UserContext:
userContext := rocketflag.UserContext{"key": "YOUR_PROTECTED_KEY"}flag, err := rf.GetFlag(flagKey, userContext)Custom Configuration
Section titled “Custom Configuration”Customize the client by passing functional options to NewClient:
rf := rocketflag.NewClient( rocketflag.WithAPIURL("https://api.custom.com"), rocketflag.WithVersion("v2"), rocketflag.WithHTTPClient(customHTTPClient),)Caching Responses
Section titled “Caching Responses”To avoid hitting the API on every check, enable an in-memory cache by providing a default TTL via WithCache. Cached entries are keyed by flag ID and the user context, so different cohorts or environments still resolve independently.
import ( "time" rocketflag "github.com/rocketflag/go-sdk")
// Enable response caching with a 5-minute default TTL.rf := rocketflag.NewClient(rocketflag.WithCache(5 * time.Minute))
// First call hits the API; subsequent calls within the TTL are served from cache.flag, err := rf.GetFlag("your-flag-id", rocketflag.UserContext{})You can override the TTL for a single call — or disable caching for that call — with WithCallTTL:
// Force a fresh fetch, bypassing the cache for this call.flag, err := rf.GetFlag("your-flag-id", nil, rocketflag.WithCallTTL(0))
// Use a shorter TTL just for this call.flag, err := rf.GetFlag("your-flag-id", nil, rocketflag.WithCallTTL(10*time.Second))Caching is opt-in — without WithCache or a per-call override, every call goes directly to the API.