Documentation
¶
Overview ¶
Package greenflags is the official Go SDK for GreenFlags feature flags.
Snapshot + cache model: one network call (Refresh) fetches every flag of the token's environment; every read after that is served from memory. See https://greenflags.dev/docs/ for the API reference.
Index ¶
- func AssignVariant(flagKey, userKey string, variants []WeightedVariant) (string, bool)
- func GeoDistanceMeters(a, b Coordinates) float64
- func IsIncludedInRollout(flagKey, userKey string, percentage int) bool
- func RolloutBucket(flagKey, userKey string) int
- type Client
- func (c *Client) AllFlags() []Flag
- func (c *Client) BoolFlag(key string, def bool) bool
- func (c *Client) GetFlag(key string) (any, bool)
- func (c *Client) GetFlagForUser(key, user string) (any, bool)
- func (c *Client) IsEnabled(key string) bool
- func (c *Client) IsEnabledForUser(key, user string) bool
- func (c *Client) JSONFlag(key string, def map[string]any) map[string]any
- func (c *Client) NumberFlag(key string, def float64) float64
- func (c *Client) Refresh(ctx context.Context) error
- func (c *Client) SetCoordinates(coords *Coordinates)
- func (c *Client) Snapshot() map[string]Flag
- func (c *Client) StartPolling(ctx context.Context, interval time.Duration)
- func (c *Client) StopPolling()
- func (c *Client) StringFlag(key string, def string) string
- func (c *Client) Subscribe(fn func(map[string]Flag)) (unsubscribe func())
- type Coordinates
- type Error
- type Flag
- type FlagType
- type FlagVariant
- type Geofence
- type Options
- type Rollout
- type WeightedVariant
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignVariant ¶ added in v0.3.0
func AssignVariant(flagKey, userKey string, variants []WeightedVariant) (string, bool)
AssignVariant assigns a user to a weighted variant: cumulative ranges over the 0-99 bucket, variants sorted by name (UTF-8 byte order — Go's native string comparison). It returns the variant name and true, or "" and false when the bucket falls beyond the total weight (base value applies).
func GeoDistanceMeters ¶ added in v0.3.0
func GeoDistanceMeters(a, b Coordinates) float64
GeoDistanceMeters returns the great-circle distance between a and b in meters (haversine formula). This is the exact calculation the SDK runs internally to evaluate geofenced flags, exposed so callers can show the live distance to a geofence without reimplementing it.
func IsIncludedInRollout ¶ added in v0.3.0
IsIncludedInRollout reports whether the user is inside the rollout percentage for the flag.
func RolloutBucket ¶ added in v0.3.0
RolloutBucket returns the 0-99 bucket a user falls into for a given flag. Stable for the same flagKey + userKey pair across every GreenFlags SDK and the server.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client reads GreenFlags feature flags. It is safe for concurrent use: one Client can be shared across goroutines. Every read path goes through geofence evaluation — the raw cached snapshot is never exposed.
func NewClient ¶
NewClient builds a Client. It performs no network request — call Refresh to load the first snapshot.
func (*Client) BoolFlag ¶
BoolFlag returns the flag's value as a bool, or def when the flag is missing or not a bool.
func (*Client) GetFlag ¶
GetFlag returns the evaluated value of key and whether the flag exists. Local read; never errors for missing flags.
func (*Client) GetFlagForUser ¶ added in v0.3.0
GetFlagForUser returns the evaluated value of key for a specific end user, resolving percentage rollout and variants with the given user key (see docs/rollout-hash-spec.md). Server processes serve many users, so identity is per call — there is no SetUser. Chain: geofence (client coordinates) → variants → rollout. Local read; never errors for missing flags.
func (*Client) IsEnabled ¶
IsEnabled returns true only when key exists and currently evaluates to true.
func (*Client) IsEnabledForUser ¶ added in v0.3.0
IsEnabledForUser returns true only when key exists and evaluates to true for the given user.
func (*Client) JSONFlag ¶
JSONFlag returns the flag's value as a map, or def when the flag is missing or not a JSON object.
func (*Client) NumberFlag ¶
NumberFlag returns the flag's value as a float64 (JSON numbers), or def when the flag is missing or not a number.
func (*Client) Refresh ¶
Refresh performs one billable request (GET /v1/flags), replaces the local snapshot and notifies subscribers. On error the previous snapshot is kept.
func (*Client) SetCoordinates ¶
func (c *Client) SetCoordinates(coords *Coordinates)
SetCoordinates sets (or clears, with nil) the end-user location used for geofence evaluation. No network request is made.
func (*Client) Snapshot ¶
Snapshot returns a copy of the evaluated snapshot, keyed by flag key. Local read — never hits the network.
func (*Client) StartPolling ¶
StartPolling refreshes every interval on a background goroutine until StopPolling is called or ctx is canceled. Errors are swallowed: the previous snapshot stays available. Every tick is one billable request.
func (*Client) StopPolling ¶
func (c *Client) StopPolling()
StopPolling stops the polling goroutine. The in-memory snapshot is kept.
func (*Client) StringFlag ¶
StringFlag returns the flag's value as a string, or def when the flag is missing or not a string.
type Coordinates ¶
Coordinates is a geographic point in decimal degrees.
type Error ¶
Error is returned on network or API failures. Code mirrors the API error codes (e.g. INVALID_TOKEN, QUOTA_EXCEEDED) plus the client-side NETWORK_ERROR and PARSE_ERROR. Status is the HTTP status (0 for network failures).
type Flag ¶
type Flag struct {
Key string `json:"key"`
Type FlagType `json:"type"`
Value any `json:"value"`
Geofence *Geofence `json:"geofence,omitempty"`
Rollout *Rollout `json:"rollout,omitempty"`
Variants []FlagVariant `json:"variants,omitempty"`
}
Flag is a feature flag as served by GET /v1/flags. Value holds bool, string, float64, map[string]any or nil depending on Type (and geofence evaluation).
type FlagType ¶
type FlagType string
FlagType is the value type of a flag: "boolean", "string", "number" or "json".
type FlagVariant ¶ added in v0.3.0
type FlagVariant struct {
Name string `json:"name"`
Weight int `json:"weight"`
Value any `json:"value"`
}
FlagVariant is a weighted variant of a multivariate flag.
type Geofence ¶
type Geofence struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
RadiusMeters float64 `json:"radiusMeters"`
}
Geofence is the geographic scope of a flag value: inside the radius the flag keeps its value, outside it evaluates to its off value.
type Options ¶
type Options struct {
// URL is the API base URL (e.g. https://app.greenflags.dev), trailing
// slash optional.
URL string
// APIToken is the environment-scoped token (gf_...) created in the
// dashboard. It determines which flags the client sees.
APIToken string
// Coordinates optionally sets the end-user location used for geofence
// evaluation (see SetCoordinates).
Coordinates *Coordinates
// HTTPClient optionally overrides the HTTP client (custom timeouts,
// proxies, or mocking). Defaults to a client with a 10s timeout.
HTTPClient *http.Client
}
Options configures a Client. URL and APIToken are required.
type Rollout ¶ added in v0.3.0
type Rollout struct {
Percentage int `json:"percentage"`
}
Rollout is a percentage rollout rule: Percentage% of users (bucketed deterministically per docs/rollout-hash-spec.md) receive the flag's value.
type WeightedVariant ¶ added in v0.3.0
WeightedVariant is a (name, weight) pair for variant assignment.