Go Integration Guide

Go Feature Flags

Feature flags for Go services — simple HTTP API, no CGo, no SDK lock-in

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

go get github.com/flagbit/flagbit-go

3. Evaluate flags

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

Use Cases for Go

Microservice Rollouts

Roll out new gRPC service versions behind flags — test in production with real traffic patterns.

Database Migration

Dual-write to old and new databases, compare results, then cut over when confident.

Algorithm Switching

A/B test sorting, ranking, or recommendation algorithms with percentage-based rollouts.

Infrastructure Changes

Switch between Redis and Memcached, or between database replicas, without redeploying.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Go app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

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.

Start using feature flags in Go

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key