Feature flags for Go services — simple HTTP API, no CGo, no SDK lock-in
Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.
go get github.com/flagbit/flagbit-go
package main
import (
"bytes"
"encoding/json"
"net/http"
"os"
"time"
)
var flagbitClient = &http.Client{Timeout: 5 * time.Second}
var sdkKey = os.Getenv("FLAGBIT_SDK_KEY")
type FlagRequest struct {
FlagKey string `json:"flag_key"`
Context map[string]string `json:"context,omitempty"`
}
type FlagResponse struct {
Value bool `json:"value"`
}
func GetFlag(flagKey string, ctx map[string]string) bool {
body, _ := json.Marshal(FlagRequest{FlagKey: flagKey, Context: ctx})
req, _ := http.NewRequest("POST",
"https://flagbit.anethoth.com/api/v1/evaluate",
bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SDK-Key", sdkKey)
resp, err := flagbitClient.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
var result FlagResponse
json.NewDecoder(resp.Body).Decode(&result)
return result.Value
}
// Gin middleware
func FeatureFlag(flagKey string) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := map[string]string{"user_id": c.GetString("user_id")}
c.Set("flag:"+flagKey, GetFlag(flagKey, ctx))
c.Next()
}
}
// Usage
func main() {
r := gin.Default()
r.GET("/api/search", FeatureFlag("new-search"), func(c *gin.Context) {
if c.GetBool("flag:new-search") {
c.JSON(200, newSearchHandler(c))
return
}
c.JSON(200, legacySearchHandler(c))
})
r.Run(":8080")
}
Roll out new gRPC service versions behind flags — test in production with real traffic patterns.
Dual-write to old and new databases, compare results, then cut over when confident.
A/B test sorting, ranking, or recommendation algorithms with percentage-based rollouts.
Switch between Redis and Memcached, or between database replicas, without redeploying.
Define flags in your FlagBit dashboard with targeting rules and rollout percentages.
Call the evaluate endpoint from your Go app with user context for targeted rollouts.
Enable, disable, or adjust rollouts in real-time. No redeployment needed.
FlagBit is a simple HTTP API — the code above is all you need. No CGo, no heavy SDK, just net/http.
Use sync.Map with a TTL, or a library like patrickmn/go-cache. Cache for 30-60 seconds to balance freshness and performance.
Yes — call FlagBit from gRPC interceptors to gate service methods behind flags.
The HTTP client is goroutine-safe. The example above works correctly in concurrent Go programs.
Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.
Get Your Free API Key