initialize performs a network request. After initialize completes, virtually all SDK operations are synchronous (refer to Evaluating Feature Gates in the Statsig SDK). The SDK fetches updates from Statsig in the background, independent of your API calls.
Working with the SDK
Checking a Feature Flag/Gate
After the SDK is initialized, you can fetch a Feature Gate. Feature Gates create logic branches in code that you can roll out to different users from the Statsig Console. Gates are CLOSED or OFF (equivalent to return false;) by default.All APIs require a user object (refer to Statsig user). To check a gate for a user:
go
user, _ := statsig.NewUserBuilderWithUserID("a-user").Build()// Basic checkif s.CheckGate(user, "a_gate") { // Gate is on, enable new feature} else { // Gate is off}// With options (e.g., disable automatic exposure logging)options := &statsig.FeatureGateEvaluationOptions{DisableExposureLogging: true}s.CheckGateWithOptions(user, "a_gate", options)
Reading a Dynamic Config
Feature Gates are useful for simple on/off switches with optional user targeting. To send different values (strings, numbers, etc.) to clients based on user attributes such as country, use Dynamic Configs. The API is similar to Feature Gates, but returns a JSON object from which you can retrieve typed parameters. For example:
go
user, _ := statsig.NewUserBuilderWithUserID("a-user").Build()// You can disable exposure logging for this specific checkdynamicConfigOptions := &statsig.DynamicConfigEvaluationOptions{DisableExposureLogging: false}dynamic_config := "a_config"config := s.GetDynamicConfigWithOptions(user, dynamic_config, dynamicConfigOptions)// Access the config valuesstr_val := config.GetString("str_val", "default_str_val") // returns Stringint_val := config.GetNumber("number_val", 100) // returns float64bool_val := config.GetBool("bool_val", false) // returns boolinterface_val := config.GetSlice("interface_val", []any{"default_interface_val"}) // returns []anymap_val := config.GetMap("map_val", map[string]any{"default": "value"}) // returns map[string]interface{}// The config object also provides metadata about the evaluationfmt.Println(config.RuleID) // The ID of the rule that served this configfmt.Println(config.IDType) // The type of the evaluation (experiment, config, etc)
Getting a Layer/Experiment
Layers/Experiments let you run A/B/n experiments. Two APIs are available, but layers are recommended because layers make parameters reusable and support mutually exclusive experiments.
After setting up a Feature Gate or Experiment, track custom events to measure how features or experiment groups affect user behavior. Call the Log Event API and specify the user and event name. You can also provide a value and metadata:
By default, the SDK automatically logs an exposure event when you check a gate, get a config, get an experiment, or call get() on a layer parameter. To delay exposure logging until the user actually uses the feature, use manual exposures.
All main SDK functions (CheckGate, GetDynamicConfig, GetExperiment, GetLayer) accept an optional options parameter with a DisableExposureLogging field. When set to true, the SDK doesn't automatically log an exposure event. You can then manually log the exposure using the corresponding manual exposure logging method:
The StatsigUser object represents a user in Statsig. You must provide a userID or at least one of the customIDs to identify the user.
When calling APIs that require a user, pass as much information as possible to enable advanced gate and config conditions (such as country or OS/browser checks) and to measure experiment impact accurately. As explained in why an ID is always required for server SDKs, at least one identifier (userID or customID) is required to provide a consistent experience for a given user.
In addition to userID, the top-level fields on StatsigUser are: email, ip, userAgent, country, locale, and appVersion. You can also pass key-value pairs in the custom field for targeting.
Private attributes
Private attributes are user attributes used for evaluation but not forwarded to any integrations. Use them for PII or sensitive data that you don't want to send to third-party services.
Statsig Options
You can pass an optional options parameter in addition to sdkKey during initialization to customize the Statsig client.
Parameters
SpecsUrlstring
Custom URL for fetching feature specifications.
LogEventUrlstring
Custom URL for logging events.
Environmentstring
Environment parameter for evaluation.
EventLoggingFlushIntervalMsnumber
How often events are flushed to Statsig servers (in milliseconds).
EventLoggingMaxQueueSizenumber
Maximum number of events to queue before forcing a flush.
SpecsSyncIntervalMsnumber
How often the SDK updates specifications from Statsig servers (in milliseconds).
OutputLogLevelstring
Controls the verbosity of SDK logs.
DisableCountryLookupboolean
Disables country lookup based on IP address. Set to true to improve performance if country-based targeting is not needed.
DisableUserAgentParsingboolean
Disables user agent parsing. Set to true to improve performance if device/browser-based targeting is not needed.
WaitForCountryLookupInitboolean
When set to true, the SDK waits for country lookup data (such as GeoIP or YAML files) to fully load during initialization. This may add approximately 1 second to startup time, but ensures that IP-to-country parsing is ready at evaluation time.
WaitForUserAgentInitboolean
When set to true, the SDK waits until user agent parsing data is fully loaded during initialization. This may add approximately 1 second to startup time, but ensures that parsing of the user's userAgent string into fields such as browserName, browserVersion, systemName, systemVersion, and appVersion is ready before any evaluations.
EnableIdListsboolean
Enables downloading ID lists; set to false to skip ID list syncing.
IdListsUrlstring
Custom URL for fetching ID lists.
IdListsSyncIntervalMsnumber
How often the SDK syncs ID lists (in milliseconds).
DisableAllLoggingboolean
When set to true, the SDK doesn't log any events or exposures.
DisableNetworkboolean
When set to true, the SDK doesn't make any network requests.
GlobalCustomFieldsstring
JSON string of custom fields that should be appended to every evaluation.
ObservabilityClientRefnumber
Internal reference for a custom observability client created through the SDK.
DataStoreRefnumber
Internal reference for a custom data store adapter created through the SDK.
PersistentStorageRefnumber
Internal reference for a persistent storage adapter created through the SDK.
InitTimeoutMsnumber
Maximum time in milliseconds to wait for SDK initialization to complete. If initialization takes longer than this timeout, the SDK will continue to operate but may return default values until initialization completes.
FallbackToStatsigApiboolean
When set to true, the SDK falls back to using the Statsig API directly if custom adapters (like local file adapters) fail to load configurations.
Proxy and custom network routing
The Go Server Core SDK currently documents endpoint overrides rather than a dedicated outbound proxy config. Use WithSpecsUrl(...), WithLogEventUrl(...), and WithIdListsUrl(...) to route Statsig network calls through your own endpoints or proxy layer.
Because events are batched and periodically flushed, some events may not have been sent when your app or server shuts down. To ensure all logged events are flushed, call shutdown() before shutting down your app or server:
go
// Method signaturefunc (s *Statsig) Shutdown() {}// example usages, err := statsig.NewStatsig("secret-key")s.Initialize()s.Shutdown()
Flush events
go
// Method signaturefunc (s *Statsig) FlushEvents() {}// example usages, err := statsig.NewStatsig("secret-key")s.Initialize()s.FlushEvents()
FAQ
Installation FAQs
How do I fix undefined symbol errors/linker errors?
You may need to reset your environment variables to those found in the statsig.env or statsig.env.ps1 file generated during the post-install script. Running the command sets the environment variables for the current terminal session. Add the variables to your .bashrc, .zshrc, or .ps1 file to load them automatically.