Set up Go SDK

Install the package

  • 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.

  • Alternatively, you can run the following command:
Shell
$go get github.com/launchdarkly/go-server-sdk/v7

Initialize the SDK

Go SDK initialization
1package main
2
3import (
4 "fmt"
5 "os"
6 "time"
7
8 "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
9 ld "github.com/launchdarkly/go-server-sdk/v7"
10)
11
12func main() {
13 ldClient, _ := ld.MakeClient("YOUR_SDK_KEY", 5*time.Second)
14 if ldClient.Initialized() {
15 fmt.Printf("SDK successfully initialized!")
16 } else {
17 fmt.Printf("SDK failed to initialize")
18 os.Exit(1)
19 }
20
21 // A "context" is a data object representing users, devices, organizations, and
22 // other entities. You'll need this later, but you can ignore it for now.
23 context := ldcontext.NewBuilder("EXAMPLE_CONTEXT_KEY").
24 Name("Sandy").
25 Build()
26
27 // For onboarding purposes only we flush events as soon as
28 // possible so we quickly detect your connection.
29 // You don't have to do this in practice because events are automatically flushed.
30 ldClient.Flush()
31}

You can find your server-side SDK key, client-side ID, and mobile key in the Environments section of your Project settings. Use the overflow menu next to the environment name to copy or display the needed key.

The environments list with the overflow menu open.

The Environments list with the overflow menu open.

To learn more, read Initialize the client in the Go SDK reference guide.