• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

Go SDK reference

Read time: 2 minutes
Last edited: Jun 18, 2021

This reference guide documents all of the methods available in the Go SDK, and explains in detail how these methods work. If you want to dig even deeper, our 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. Additionally you can clone and run a sample application using this SDK.

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

Getting started

Building on top of our Getting Started guide, the following steps will get you started with 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"
4
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"
7
8 // go-server-sdk.v5/ldcomponents is for advanced configuration options
9 "gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
10)

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.

1ldClient, _ := 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"
3)
4
5flagKey := "some-flag-key"
6user := lduser.NewUser("some-user-key")
7showFeature, _ := ldClient.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
12}

Lastly, 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.

1// shut down the client, since we're about to quit
2ldClient.Close()

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.

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"
4)
5
6var config ld.Config
7config.HTTP = ldcomponents.HTTPConfiguration().
8 ProxyURL("https://web-proxy.domain.com:8080")

Supported features

This SDK supports the following features: