Evaluation contexts and users
An evaluation context is the input that tells the Go Feature Flag SDK who or what a flag decision is for. It carries a stable identity and the attributes that targeting rules can inspect. The current ffcontext model replaces the older ffuser model and feeds typed evaluation, percentage assignment, bulk evaluation, and event data.
Context model
Evaluation contexts
The public ffcontext package at home/…/context.go exposes EvaluationContext, the current representation of an evaluation subject. The subject can be a signed-in user, an anonymous visitor, a device, an organization, or another entity that needs a flag decision.
Every context has a targeting key and custom attributes. The targeting key is the stable identity used to keep a subject in the same percentage or progressive rollout bucket. The custom attributes describe the subject and provide the values that targeting rules can use.
The SDK accepts the smaller ffcontext.Context interface at evaluation boundaries. This keeps flag evaluation independent from one concrete context value while still giving it access to identity, anonymity, custom attributes, and GO Feature Flag control data.
References: ffcontext/context.go• L6-32 modules/…/context.go• L12-57
Legacy users
ffuser.User is the older form of the same basic subject data. It has a key, an anonymous state, and custom attributes, but its constructors and builder are deprecated in favor of ffcontext.EvaluationContext and ffcontext.EvaluationContextBuilder.
New code should create an evaluation context. Existing code can still use the user API where compatibility requires it, but new context features such as GO Feature Flag control data belong to ffcontext.
References: ffuser/user.go• L6-32 ffuser/user_builder.go• L3-23
Context construction
Direct constructors
NewEvaluationContext creates a context with a targeting key and no custom attributes. This is the simple choice when the caller has only an identity or when it will add attributes after construction.
Anonymous status is represented by the anonymous custom attribute in the current model. The older anonymous constructor remains for compatibility. New callers should use the normal constructor and add the attribute explicitly so one context model serves both anonymous and identified subjects.
References: modules/…/context.go• L28-42 ffcontext/context.go• L13-24
Context builders
NewEvaluationContextBuilder supports a fluent construction flow. The caller supplies the targeting key, adds custom attributes, and calls Build to produce the context used by an evaluation.
The builder's Anonymous method is deprecated. AddCustom("anonymous", true) is the current form because anonymity is part of the same attribute set that targeting and evaluation receive.
References: modules/…/context_builder.go• L3-23 modules/…/context_builder.go• L31-53
Attribute values
Custom attributes accept general values, including strings, numbers, Boolean values, lists, maps, and nil. They can describe user traits or request state such as a plan, region, device, or account group.
The context can be changed after creation through AddCustomAttribute. A blank name is ignored by this method. Keep attribute names stable because flag queries depend on their exact names and shape.
References: modules/…/context.go• L87-107 modules/…/context_test.go• L284-375
Evaluation inputs
Targeting attributes
The evaluator turns the context into a query input before it checks flag rules. That input contains custom attributes plus standard identity fields: key, targetingKey, and anonymous.
Only data present in this input can satisfy a targeting query. A missing attribute does not match a rule that needs it, so the application must add every value that its flag rules require.
References: modules/…/serializer.go• L7-20 modules/…/rule.go• L68-110
Shared attributes
The SDK can add shared evaluation data to every request. This data is useful for service-level facts such as the environment or server version. The evaluator merges it into the context before it selects a variation, so request-level and shared data form one targeting input.
When both sources use the same name, the shared enrichment currently writes its value into the context before rule selection. Treat attribute names shared with configuration as a contract between the application and its SDK configuration.
References: variation.go• L395-402 variation_all_flags.go• L44-65 modules/…/internal_flag.go• L65-90
Protected attributes
The gofeatureflag attribute name is reserved for SDK control data. It can carry values that affect evaluation or event output without changing the subject identity.
| Attribute | Role |
|---|---|
gofeatureflag.currentDateTime | Sets the time used by date-based rollout decisions. |
gofeatureflag.flagList | Limits a bulk evaluation to a selected set of flags. |
gofeatureflag.exporterMetadata | Adds caller-supplied metadata to exported evaluation events. |
Do not use gofeatureflag for ordinary targeting data. The SDK reads this name as control input and may ignore values with an unsupported shape.
References: modules/…/goff_context_specifics.go• L5-12 modules/…/context.go• L110-125 variation.go• L320-345
Evaluation role
Identity and bucketing
The targeting key is the default bucketing key. A flag can define another custom bucketing attribute, but that value must be present and be a usable string when the flag needs percentage or progressive assignment.
A stable key makes the assignment repeatable across evaluations. An empty key can work for rules that do not need bucketing, but a flag that needs percentage or progressive assignment cannot select a stable variation without a key.
References: modules/…/internal_flag.go• L447-487 modules/…/rule.go• L53-99
Evaluation flow
The context moves from application data to rule input and then to a flag result. The following flow shows where identity, attributes, and SDK control data affect the decision.
flowchart LR
A[Application subject] --> B[Evaluation context]
B --> C[Identity and custom attributes]
C --> D[Shared SDK enrichment]
D --> E[Targeting and bucketing input]
E --> F[Flag rule evaluation]
F --> G[Typed variation or detail result]
B --> H[Evaluation event identity and attributes]
Typed variation calls pass the same context into the evaluation engine. The engine uses its attributes for rule matching and its key for bucketing. The resulting context data also supports evaluation events and explicit tracking events.
See Feature flag SDK lifecycle for the wider request lifecycle and flag evaluation and targeting for rule resolution. See SDK configuration and retrieval for shared evaluation data and the flag state that receives it.
References: variation.go• L18-57 variation.go• L347-402 tracking.go• L10-41
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
ffcontext.Contextand every evaluation entry point aligned when identity or attribute access changes. - Recheck the normalized query input if the names
key,targetingKey, oranonymouschange. - Recheck the merge contract between request attributes and configuration enrichment when either source changes.
- Preserve the
gofeatureflagcontrol-data contract across evaluation, bulk evaluation, date-based rollouts, and event export. - Keep the deprecated
ffuserpath compatible until all callers can move toffcontext.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The targeting key is empty for a percentage or progressive rollout | The flag cannot choose a bucketing result and returns the SDK default with an error. | Provide a stable key for every subject. |
| A targeting rule uses an attribute that is not in the context | The rule does not match the subject. | Add the required attribute with the expected name and value shape. |
gofeatureflag is used as normal application data | The SDK may treat the value as control input or fail to read it. | Keep ordinary attributes outside the reserved name. |
| A custom bucketing attribute is missing or is not a string | Bucketing fails when the flag requires that attribute. | Supply a valid string at the configured attribute path. |
| An application keeps using the deprecated user model for new context features | New ffcontext behavior is not available through that construction path. | Use NewEvaluationContext or NewEvaluationContextBuilder for new code. |
Key files
| File | Why to read it |
|---|---|
| ffcontext/context.go | Defines the public context aliases and constructors used by applications. |
| modules/…/context.go | Defines context identity, anonymity, custom attributes, map conversion, and protected control data. |
| modules/…/context_builder.go | Defines the current builder flow and the anonymous compatibility method. |
| ffuser/user.go | Shows the deprecated user representation that the context model replaces. |
| ffuser/user_builder.go | Shows the deprecated user builder and its migration direction. |
| modules/…/serializer.go | Defines the context shape supplied to targeting queries. |
| modules/…/internal_flag.go | Shows how context identity and shared attributes enter flag evaluation. |
| modules/…/goff_context_specifics.go | Defines the reserved control data extracted from a context. |
| variation.go | Shows how application evaluation calls pass context into the evaluator and event path. |