Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This topic documents how to get started with the Go SDK, and links to reference information on all of the supported features.LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
| Resource | Location | ||
|---|---|---|---|
| SDK API documentation | [SDK API docs](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v | Supported SDK Versions | Go server SDK |
| GitHub repository | go-server-sdk | ||
| Sample application | Go |
The LaunchDarkly Go SDK, version 7.10 and higher, is compatible with Go 1.23 and higher.The LaunchDarkly Go SDK, version 7.0 through 7.9, is compatible with Go 1.20 and higher.The LaunchDarkly Go SDK, version 6.x, is compatible with Go 1.18 and higher.
Get started
After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Go application.Install the SDK
First, install the LaunchDarkly SDK as a dependency in your application. How you do this depends on what dependency management system you are using:- If you are using the standard Go modules system, import the SDK packages in your code and
go buildwill automatically download them. The SDK and its dependencies are modules. - Otherwise, use the
go getcommand and specify the SDK version, such asgo get github.com/launchdarkly/go-server-sdk/v7.
go-server-sdk, you should also update go-sdk-common.
Initialize the client
After you install and import the SDK, you have two options:- Create an instance of
LDScopedClient. This is a wrapper aroundLDClientthat lets you specify the evaluation context to use for all operations, so you do not need to specify a context for each method call. The scoped client’s context is a multi-context, and you can update the multi-context with additional or updated associated contexts any time. - Create a single, shared instance of
LDClient. This client requires that you specify an evaluation context in each method call.
The Go SDK uses an SDK key. Keys are specific to each project and environment. They are available from Project settings, on the Environments list. To learn more about key types, read Keys.
ld, as shown above.
MakeClient or MakeCustomClient is a timeout parameter. In these examples, you are telling the SDK that it can spend up to five seconds attempting to connect to LaunchDarkly services before returning to your application. For more details about what the timeout means and what happens if initialization fails, read MakeClient and MakeCustomClient.
To learn more about the observability plugin, read Go SDK observability reference. To learn more about the specific configuration options available for this SDK, read Config.
Evaluate a context
After you initialize the client, you can check which variation a particular context should receive for a given feature flag. If you are usingLDScopedClient, you create the scoped client with the context, and then evaluate the flag. If you are using LDClient, you pass the context to the flag evaluation method.
Here’s how:
Use Go contexts with LDScoped
Client As described above, one advantage of usingLDScopedClient is that you can specify the evaluation context to use for all operations, and do not need to specify a context for each method call. You can update the scoped client’s current context with additional or updated associated contexts any time.
After you create a scoped client, we recommend adding it your Go context. Another advantage of using LDScopedClient is that you can pass the scoped client to any logic that already takes a Go context (context.Context), using utility methods provided in the SDK. This means the scoped client is implicitly passed around through all of your code that uses context.Context, and you can access the scoped client anywhere in your application logic.
Here’s how to add and retrieve your LDScopedClient from your Go context:
LDScopedClient into the http.Request’s Go context:
GoContextWithScopedClient, GetScopedClient, and MustGetScopedClient.
HTTPS Proxy
Go’s standard HTTP library provides a built-in HTTPS proxy. If theHTTPS_PROXY environment variable is present, then the SDK will proxy all network requests through the URL provided.
Here is an example:
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.Supported features
This SDK supports the following features:- Anonymous contexts and users
- Big segments
- Configuration, including
- Context configuration
- Data saving mode
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Hooks
- Identifying and changing contexts
- Logging configuration
- Migrations
- Monitoring SDK status
- Observability
- Offline mode
- OpenTelemetry
- Private attributes
- Reading flags from a file
- Relay Proxy configuration
- Secure mode
- Sending custom events
- Shutting down
- Storing data
- Subscribing to flag changes
- Test data sources
- Web proxy configuration