LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at support@launchdarkly.com

Get Started    Documentation

Go SDK Reference

This reference guide documents all of the methods available in our Go SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source-- head to our Go SDK GitHub repository.

Getting started

If you haven't taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your Go application.

Once the SDK is installed, you'll want to create a single, shared instance of LDClient. We'll assume you've imported the LaunchDarkly SDK package as ld:

import ld "gopkg.in/launchdarkly/go-client.v2"

ld_client := ld.MakeClient("YOUR_SDK_KEY", 5 * time.Second)

LDClient must be a singleton

It's important to make this a singleton-- internally, 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.

The timeout parameter can be used to block initialization until the client has been bootstrapped.

Customizing your client

You can also pass custom parameters to the client by creating a custom configuration object. It's easiest to create one by starting with the DefaultConfig and assigning to the fields you need to customize:

config := ld.DefaultConfig
config.FlushInterval = 10 * time.Second
ldClient := ld.MakeCustomClient("YOUR_SDK_KEY", config, 5 * time.Second)

Here, we've customized the client flush interval parameter. See the Godocs for a complete list of configuration options for the client.

Users

Feature flag targeting and rollouts are all determined by the user you pass to your Variation calls. The Go SDK defines a User struct to make this easy. Here's an example:

key := "aa0ceb"
firstName := "Ernestina"
lastName := "Evans"
email := "ernestina@example.com"
custom := map[string]interface{} {
  "groups": []string{"Google", "Microsoft"},
}
user := ld.User{
  Key: &key
  FirstName: &firstName,
  LastName: &lastName,
  Email: &email,
  Custom: &custom,
}

Let's walk through this snippet. The most attribute is the user's key-- in this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like FirstName, Email, and the Custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

Besides the Key, LaunchDarkly supports the following attributes at the "top level". Remember, all of these are optional:

  • Ip: Must be an IP address. If you provide an IP, LaunchDarkly will use a geolocation service to automatically infer a country for the user (unless you've already specified one).
  • FirstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
  • LastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
  • Country: Must be a string representing the country associated with the user.
  • Email: Must be a string representing the user's e-mail address. If an avatar URL is not provided, we'll use Gravatar to try to display an avatar for the user on the Users page.
  • Avatar: Must be an absolute URL to an avatar image for the user.
  • Name: Must be a string. You can search for users on the User page by name
  • Anonymous: Must be a boolean. See the section below on anonymous users for more details.

In addition to these, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on types

Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers. This is why Custom is defined as a map from string to interface{}.

If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way. The Go SDK is strongly typed, so be aware of this distinction.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us-- organizations, groups, account plans-- anything you pass to us becomes available instantly on our dashboard.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

key := "aa0ceb"
anonymous := true

user := ld.User{
  Key: &key,
  Anonymous: &anonymous,
}

You will still need to generate a unique key for anonymous users-- session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing-- it keeps anonymous users from polluting your Users page!

Variation

The Variation method determines whether a flag is enabled or not for a specific user. In Go, there is a Variation method for each type (e.g. BoolVariation, StringVariation):

shouldShow, err := ldClient.BoolVariation("your.feature.key", user, false)

Variation calls take the feature flag key, a User, and a default value.

The default value will only be returned if an error is encountered-- for example, if the feature flag key doesn't exist or the user doesn't have a key specified.

The Variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).

Track

The Track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here's a simple example:

ldClient.Track("Signed up", user)

You can also attach custom data to your event by passing an extra parameter to Track:

data := map[string]interface{}{"price": 320}
ldClient.Track("Completed purchase", user, data)

You can attach any data that can be marshaled to JSON to your events.

Identify

The Identify method creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won't need to call Identify-- calling Variation will automatically create users on the dashboard for you. Identify can be useful if you want to pre-populate your dashboard before launching any features.

ldClient.Identify(user)

All flags

Creating users

Note that unlike variation and identify calls, AllFlags does not send events to LaunchDarkly. Thus, users are not created or updated in the LaunchDarkly dashboard.

The AllFlags method produces a map of feature flag keys to their values for a specific user. In Java, we also provide an allBoolFlags helper method that returns a map of boolean flags.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.

ldClient.AllFlags()

Secure mode hash

The SecureModeHash method computes an HMAC signature of a user signed with the client's SDK key. If you're using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.

ldClient.SecureModeHash(user)

Flush

Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK on the command line), you may want to manually call flush to process events immediately.

ldClient.Flush();

Note that the flush interval is configurable-- if you need to change the interval, you can do so by making a custom client configuration

Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. You can do this by setting Offline mode in the client's Configuration.

config := ld.DefaultConfig
config.Offline = true
ldClient := ld.MakeCustomClient("YOUR_SDK_KEY", config, 5 * time.Second)
ldClient.BoolVariation("any.feature.flag", user, false) // will always return the default value (false)

Logging

The Go SDK uses Go's built-in log package. All loggers are namespaced under [LaunchDarkly]. A custom logger may be passed to the SDK via the configurable Logger property:

config := ld.DefaultConfig
config.Logger = myLogger
ldClient := ld.MakeCustomClient("YOUR_SDK_KEY", config, 5 * time.Second)

Be aware of two considerations when enabling the DEBUG log level:

  1. Debug-level logs can be very verbose. It is not recommended that you turn on debug logging in high-volume environments.
  2. Potentially sensitive information is logged including LaunchDarkly users created by you in your usage of this SDK.

Redis

The Go SDK provides an option to use Redis as a persistent store of feature flag configurations. Here's an example client configuration hooked up to a Redis instance:

host = "localhost"
port = 6379
timeout = time.Duration(2) * time.Second
store := NewRedisFeatureStore(host, port, "", timeout, myLogger)

config := ld.DefaultConfig
config.FeatureStore = store
ldClient := ld.MakeCustomClient("YOUR_SDK_KEY", config, 5 * time.Second)

For advanced RedisFeatureStore configuration options, please see the SDK's (GoDoc)[https://godoc.org/gopkg.in/launchdarkly/go-client.v2#RedisFeatureStore].

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.

How to set the HTTPS_PROXY environment variable on Mac/Linux systems:

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

How to set the HTTPS_PROXY environment variable on Windows systems:

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

Go SDK Reference