No results for ""
EXPAND ALL
  • Home
  • API docs

Go SDK reference

Read time: 6 minutes
Last edited: Nov 14, 2024
Recent major versions

Version 7 of the Go SDK supports migration feature flags. These are temporary flags used to migrate data or systems while keeping your application available and disruption free. To learn more about upgrading, read Go SDK 6.x to 7.0 migration guide.

Version 6 of the Go SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more about upgrading, read Go SDK 5.x to 6.0 migration guide and Best practices for upgrading users to contexts.

Code samples on this page are from the three most recent SDK versions where they differ.

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:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositorygo-server-sdk
Sample applicationGo
Published modulepkg.go.dev
SDK version compatibility

The LaunchDarkly Go SDK, version 7.0 and higher, is compatible with Go 1.20 and higher.

The LaunchDarkly Go SDK, version 6.x, is compatible with Go 1.18 and higher.

The LaunchDarkly Go SDK, version 5.x, is compatible with Go 1.14 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.
  • If you are using dep, import the SDK packages in your code and run dep ensure.
  • Otherwise, use the go get command and specify the SDK version, such as go get github.com/launchdarkly/go-server-sdk/v6.

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

import (
// go-sdk-common/v3/ldcontext defines LaunchDarkly's model for contexts
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
// go-sdk-common/v3/ldmigration defines LaunchDarkly's model for migration feature flags
"github.com/launchdarkly/go-sdk-common/v3/ldmigration"
// go-server-sdk/v7 is the main SDK package - here we are aliasing it to "ld"
ld "github.com/launchdarkly/go-server-sdk/v7"
// go-server-sdk/v7/ldcomponents is for advanced configuration options
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
)

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.

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 the Environments list for each project. To learn more about key types, read Keys.

Initialize the client

After you install and import the SDK, create a single, shared instance of LDClient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

LDClient must be a singleton

It's important to make 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 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.

client, _ := ld.MakeClient("sdk-key-123abc", 5*time.Second)

The second argument to MakeClient is a timeout parameter. In this example, 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.

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

Using the LDClient methods, you can check which variation a particular context should receive for a given feature flag. To learn more, read Evaluating flags and Flag evaluation reasons. For more information about how contexts are specified, read Context configuration.

In the v6 example, the context key is the string "context-key-123abc". In the v5 example, the user key is the string "user-key-123abc":

import (
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
)
flagKey := "flag-key-123abc"
context := ldcontext.NewBuilder("context-key-123abc").
Name("Sandy").
Build()
showFeature, _ := client.BoolVariation(flagKey, context, false)
if showFeature {
// Application code to show the feature
} else {
// The code to run if the feature is off
}

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:

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

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

var config ld.Config
config.HTTP = ldcomponents.HTTPConfiguration().
ProxyURL("https://web-proxy.domain.com:8080")

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: