• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""


Go SDK reference

Read time: 2 minutes
Last edited: Oct 26, 2021


This reference guide documents all of the methods available in the Go SDK, and explains in detail how these methods work. LaunchDarkly's SDKs are open source. To learn more, read Go SDK GitHub repository. The online Go API docs contain the programmatic definitions of every type and method. You can also try this SDK out by cloning and running a sample application.

Go version compatibility

Version 5.0 and higher of the LaunchDarkly Go SDK is compatible with Go 1.14 and higher.

Getting started

After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Go application.

The first step is to install the LaunchDarkly SDK as a dependency in your application. How you do this depends on what dependency management system you are using:

  • For the standard Go modules system, you can simply import the SDK packages in your code and go build will automatically download them. The SDK and its dependencies are modules.
  • If you are using dep, import the SDK packages in your code and run dep ensure.
  • Or you can use the go get command (for instance, go get gopkg.in/launchdarkly/go-server-sdk.v5).

There are several packages that you can import, depending on which features you are using. You will usually need the following:

1import (
2 // go-sdk-common.v2/lduser defines LaunchDarkly's model for user properties
3 "gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
5 // go-server-sdk.v5 is the main SDK package - here we are aliasing it to "ld"
6 ld "gopkg.in/launchdarkly/go-server-sdk.v5"
8 // go-server-sdk.v5/ldcomponents is for advanced configuration options
9 "gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"

It is usually a good practice to pin your dependencies to a specific version. Refer to the SDK releases page to identify the latest version. Whenever you update your version of go-server-sdk, you should also update go-sdk-common.

Once the SDK is installed and imported, you'll want to create a single, shared instance of the LaunchDarkly client (LDClient). You must specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

We'll assume you've imported the LaunchDarkly SDK package as ld, as shown above.

1client, _ := ld.MakeClient("YOUR_SDK_KEY", 5 * time.Second)

The second argument to MakeClient is a timeout parameter: in this case, you are telling the SDK that it can spend up to 5 seconds attempting to connect to LaunchDarkly services before returning to your application. See MakeClient for more details about what the timeout means and what happens if initialization fails.

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.

LDClient must be a singleton

It's important to make this a singleton. The client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you're not instantiating a new client with every request.

Using the LDClient methods, you can check which variation a particular user should receive for a given feature flag. See Evaluating flags and Flag evaluation reasons for more details. See User configuration for more about how user properties are specified-- in this example, there is a user key that consists of an email address.

1import (
2 "gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
5flagKey := "some-flag-key"
6user := lduser.NewUser("some-user-key")
7showFeature, _ := client.BoolVariation(flagKey, user, false)
8if showFeature {
9 // application code to show the feature
10} else {
11 // the code to run if the feature is off

When your application is about to terminate, shut down the LDClient with Close(). This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.

Here is an example:

1// shut down the client, since we're about to quit


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:

1export HTTPS_PROXY=https://web-proxy.domain.com:8080

You can also specify a proxy programmatically through the SDK configuration:

1import (
2 ld "gopkg.in/launchdarkly/go-server-sdk.v5"
3 "gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
6var config ld.Config
7config.HTTP = ldcomponents.HTTPConfiguration().
8 ProxyURL("https://web-proxy.domain.com:8080")

Lastly, shut down the client when your application terminates. To learn more, read Shutting down.

Supported features

This SDK supports the following features: