Go SDK reference
Overview
This topic documents how to get started with the Go SDK, and links to reference information on all of the supported features.
SDK quick links
LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
SDK version compatibility
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 build
will automatically download them. The SDK and its dependencies are modules. - Otherwise, use the
go get
command and specify the SDK version, such asgo get github.com/launchdarkly/go-server-sdk/v7
.
There are several packages that you can import, depending on which features you are using. We recommend following:
It is good practice to pin your dependencies to a specific version. Refer to the SDK releases page to identify the latest version. When you update your version of 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 aroundLDClient
that 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.
LDScopedClient is in beta
LDScopedClient
is in beta. It is still undergoing testing and active development. Its functionality may change without notice, including becoming backwards incompatible.
Both options require that you specify your SDK key. The SDK key authorizes your application to connect to a particular environment within LaunchDarkly.
The Go SDK uses an SDK key
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.
LDClient must be a singleton
It’s important to make the LDClient
a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.
If you use LDScopedClient
, you should create a new scoped client for each logical scope for your context or multi-context. For example, you might create a new scoped client for each web request. Each scoped client should use the same LDClient
instance in its initialization.
If you have multiple LaunchDarkly projects, you can create one LDClient
for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
This example assumes you’ve imported the LaunchDarkly SDK package as ld
, as shown above.
The final argument to 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
.
Best practices for error handling
The second return type in these code samples ( _
) represents an error in case the LaunchDarkly client does not initialize. Consider naming the return value and using it with proper error handling.
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 using LDScopedClient
, 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:
To learn more, read Evaluating flags and Flag evaluation reasons. For more information about how contexts are specified, read Context configuration.
Use Go contexts with LDScopedClient
As described above, one advantage of using LDScopedClient
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:
An example of how this might apply in your application is if you have HTTP middleware, and you want to pass an LDScopedClient
into the http.Request
’s Go context:
To learn more, read GoContextWithScopedClient
, GetScopedClient
, and MustGetScopedClient
.
HTTPS Proxy
Go’s standard HTTP library provides a built-in HTTPS proxy. If the HTTPS_PROXY
environment variable is present, then the SDK will proxy all network requests through the URL provided.
Here is an example:
You can also specify a proxy programmatically through the SDK configuration:
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