# Optimizely Go SDK [![Go Report Card](https://goreportcard.com/badge/github.com/optimizely/go-sdk)](https://goreportcard.com/report/github.com/optimizely/go-sdk) [![Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0) This repository houses the Go SDK for use with Optimizely Feature Experimentation and Optimizely Full Stack (legacy). Optimizely Feature Experimentation is an A/B testing and feature management tool for product development teams that enables you to experiment at every step. Using Optimizely Feature Experimentation allows for every feature on your roadmap to be an opportunity to discover hidden insights. Learn more at [Optimizely.com](https://www.optimizely.com/products/experiment/feature-experimentation/), or see the [developer documentation](https://docs.developers.optimizely.com/experimentation/v4.0.0-full-stack/docs/welcome). Optimizely Rollouts is [free feature flags](https://www.optimizely.com/free-feature-flagging/) for development teams. You can easily roll out and roll back features in any application without code deploys, mitigating risk for every feature on your roadmap. ## Get Started Refer to the [Go SDK's developer documentation](https://docs.developers.optimizely.com/experimentation/v4.0.0-full-stack/docs/go-sdk) for detailed instructions on getting started with using the SDK. ### Requirements Requires Golang version: | SDK Version | Required Golang Version | |--------------|--------------------------| | v2.0.0 | 1.21.0 or higher | | < v2.0.0 | 1.13 or higher | ### Install the SDK #### Install from github: ```$sh go get github.com/optimizely/go-sdk/v2 ``` #### Install from source: ```$sh go get github.com/optimizely/go-sdk/v2 cd $GOPATH/src/github.com/optimizely/go-sdk/v2 go install ``` NOTE: We practice trunk-based development, and as such our default branch, `master` might not always be the most stable. We do tag releases on Github and you can pin your installation to those particular release versions. One way to do this is to use [*Go Modules*](https://blog.golang.org/using-go-modules) for managing external dependencies: #### Install using go.mod: ``` module mymodule go 1.21.0 require ( github.com/optimizely/go-sdk/v2 v2.0.0 ) ``` If you are already using `go.mod` in your application you can run the following: ``` go mod edit -require github.com/optimizely/go-sdk/v2@v2.0.0 ``` NOTE: ```$sh go get github.com/optimizely/go-sdk/v2/... ``` or ```$sh go get github.com/optimizely/go-sdk/v2/pkg ``` will install it as a package to pkg directory, rather than src directory. It could be useful for future development and vendoring. ### Feature Management Access To access the Feature Management configuration in the Optimizely dashboard, please contact your Optimizely customer success manager. ## Use the Go SDK See the example file in examples/main.go. ### Initialization ``` import optly "github.com/optimizely/go-sdk/v2" import "github.com/optimizely/go-sdk/v2/client" // Simple one-line initialization with the SDK key    optlyClient, err := optly.Client("SDK_KEY")    // You can also instantiate with a hard-coded datafile using our client factory method    optimizelyFactory := &client.OptimizelyFactory {        Datafile: []byte("datafile_string"),    }    optlyClient, err = optimizelyFactory.Client() ``` ### Make Decisions ``` import ( "fmt" optly "github.com/optimizely/go-sdk/v2" "github.com/optimizely/go-sdk/v2/pkg/decide" ) // instantiate a client client, err := optly.Client("SDK_KEY") // User attributes are optional and used for targeting and results segmentation attributes := map[string]interface{}{ "state": "California", "likes_donuts": true, } user := client.CreateUserContext("optimizely end user", attributes) options := []decide.OptimizelyDecideOptions{decide.IncludeReasons} decision := user.Decide("my_flag", options) var variationKey string if variationKey = decision.VariationKey; variationKey == "" { fmt.Printf("[decide] error: %v", decision.Reasons) return } if variationKey == "control" { // Execute code for control variation } else if variationKey == "treatment" { // Execute code for treatment variation } ``` ## SDK Development ### Unit Tests Run ``` make test ``` ### Contributing Please see [CONTRIBUTING](https://github.com/optimizely/go-sdk/blob/master/CONTRIBUTING.md). ### Credits This software is distributed with code from the following open source projects: murmur3 Copyright 2013, Sébastien Paolacci. License (BSD-3 Clause): https://github.com/twmb/murmur3/blob/master/LICENSE uuid Copyright (c) 2009, 2014 Google Inc. All rights reserved. License (BSD-3 Clause): https://github.com/google/uuid/blob/master/LICENSE testify Copyright (c) 2012-2018 Mat Ryer and Tyler Bunnell. License (MIT): https://github.com/stretchr/testify/blob/master/LICENSE json-iterator Copyright (c) 2016 json-iterator License (MIT): https://github.com/json-iterator/go/blob/master/LICENSE subset Copyright (c) 2015, Facebook, Inc. All rights reserved. License (BSD): https://github.com/facebookarchive/subset/blob/master/license profile Copyright (c) 2013 Dave Cheney. All rights reserved. License (BSD): https://github.com/pkg/profile/blob/master/LICENSE sync Copyright (c) 2009 The Go Authors. All rights reserved. https://github.com/golang/sync/blob/master/LICENSE opentelemetry-go Copyright The OpenTelemetry Authors License (Apache-2.0): https://github.com/open-telemetry/opentelemetry-go/blob/main/LICENSE go-multierror Copyright IBM Corp. 2014, 2025 License (MPL-2.0): https://github.com/hashicorp/go-multierror/blob/master/LICENSE errwrap (dependency of go-multierror) Copyright IBM Corp. 2014, 2025 License (MPL-2.0): https://github.com/hashicorp/errwrap/blob/master/LICENSE The MPL-2.0 licensed packages above are used unmodified as Go module dependencies. MPL-2.0 is a file-level (weak) copyleft license: its terms apply to those packages' own source files and do not change the Apache-2.0 license of this SDK's code. If you distribute a compiled binary that includes these packages, MPL-2.0 (section 3.2) requires you to make the source code of those packages available and to tell recipients how to obtain it. The source is published at the links above. See the MPL-2.0 license text for the full terms. ### Other Optimizely SDKs - Agent - https://github.com/optimizely/agent - Android - https://github.com/optimizely/android-sdk - C# - https://github.com/optimizely/csharp-sdk - Flutter - https://github.com/optimizely/optimizely-flutter-sdk - Java - https://github.com/optimizely/java-sdk - JavaScript - https://github.com/optimizely/javascript-sdk - PHP - https://github.com/optimizely/php-sdk - Python - https://github.com/optimizely/python-sdk - React - https://github.com/optimizely/react-sdk - Ruby - https://github.com/optimizely/ruby-sdk - Swift - https://github.com/optimizely/swift-sdk