{"id":19149842,"url":"https://github.com/harness/ff-golang-server-sdk","last_synced_at":"2025-05-07T04:45:41.929Z","repository":{"id":37438294,"uuid":"343506760","full_name":"harness/ff-golang-server-sdk","owner":"harness","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-16T23:24:42.000Z","size":727,"stargazers_count":11,"open_issues_count":2,"forks_count":16,"subscribers_count":60,"default_branch":"main","last_synced_at":"2025-05-07T04:45:27.536Z","etag":null,"topics":["feature-flags","feature-toggles","go","golang","hacktoberfest","sdk"],"latest_commit_sha":null,"homepage":"https://harness.io/products/feature-flags/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/harness.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-03-01T17:51:02.000Z","updated_at":"2025-03-27T16:18:31.000Z","dependencies_parsed_at":"2023-10-31T15:28:44.475Z","dependency_job_id":"e8a9de6b-1fbf-4a62-81e3-32b9c1ba1539","html_url":"https://github.com/harness/ff-golang-server-sdk","commit_stats":null,"previous_names":["drone/ff-golang-server-sdk"],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harness%2Fff-golang-server-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harness%2Fff-golang-server-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harness%2Fff-golang-server-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harness%2Fff-golang-server-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harness","download_url":"https://codeload.github.com/harness/ff-golang-server-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252816520,"owners_count":21808702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["feature-flags","feature-toggles","go","golang","hacktoberfest","sdk"],"created_at":"2024-11-09T08:09:49.745Z","updated_at":"2025-05-07T04:45:41.910Z","avatar_url":"https://github.com/harness.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Golang SDK For Harness Feature Flags\n========================\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/drone/ff-golang-server-sdk)](https://goreportcard.com/report/github.com/drone/ff-golang-server-sdk)\n\n## Table of Contents\n**[Intro](#Intro)**\u003cbr\u003e\n**[Requirements](#Requirements)**\u003cbr\u003e\n**[Quickstart](#Quickstart)**\u003cbr\u003e\n**[Further Reading](docs/further_reading.md)**\u003cbr\u003e\n**[Build Instructions](docs/build.md)**\u003cbr\u003e\n\n\n## Intro\nUse this README to get started with our Feature Flags (FF) SDK for Golang. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out.\nThis sample doesn’t include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the  [Golang SDK Reference](https://ngdocs.harness.io/article/4c8wljx60w-feature-flag-sdks-go-application).\n\nFor a sample FF Golang SDK project, see our [test Golang project](examples/getting_started.go).\n\n![FeatureFlags](./docs/images/ff-gui.png)\n\n## Requirements\n- Starting with SDK version v0.1.21, Golang version 1.20 or later is required.\n- Earlier versions of the SDK require Golang versions newer than 1.6 but older than 1.19. \n- For installation details, please refer to [Golang's official installation guide](https://go.dev/doc/install).\n## Quickstart\nTo follow along with our test code sample, make sure you’ve:\n\n- [Created a Feature Flag on the Harness Platform](https://ngdocs.harness.io/article/1j7pdkqh7j-create-a-feature-flag) called harnessappdemodarkmode\n- [Created a server SDK key and made a copy of it](https://ngdocs.harness.io/article/1j7pdkqh7j-create-a-feature-flag#step_3_create_an_sdk_key)\n\n\n## Install the SDK\nInstall the golang SDK using go\n```golang\ngo get github.com/harness/ff-golang-server-sdk\n```\n\n## Initialize the SDK\n### Non-blocking Initialization (Default)\nBy default, when initializing the Harness Feature Flags client, the initialization process is non-blocking. This means that the client creation call will return immediately, allowing your application to continue its startup process without waiting for the client to be fully initialized. Here’s an example of creating a non-blocking client:\n\n```go\nclient, err := harness.NewCfClient(apiKey)\n```\nIn this scenario, the client will initialize in the background, making it possible to use the client even if it hasn’t finished initializing. \nBe mindful that if you attempt to evaluate a feature flag before the client has fully initialized, it will return the default value provided in the evaluation call.\n\n### Blocking Initialization\nIn some cases, you may want your application to wait for the client to finish initializing before continuing. To achieve this, you can use the `WithWaitForInitialized` option, which will block until the client is fully initialized. Example usage:\n\n```go\nclient, err := harness.NewCfClient(sdkKey, harness.WithWaitForInitialized(true))\n\nif err != nil {\nlog.ErrorF(\"could not connect to FF servers %s\", err)\n}\n```\n\n\nIn this example, WaitForInitialized will block for up to 5 authentication attempts. If the client is not initialized within 5 authentication attempts, it will return an error.\n\nThis can be useful if you need to unblock after a certain time. **NOTE**: if you evaluate a feature flag in this state\nthe default variation will be returned.\n\n```go\n// Try to authenticate only 5 times before returning a result\nclient, err := harness.NewCfClient(sdkKey, harness.WithWaitForInitialized(true), harness.WithMaxAuthRetries(5))\n\nif err != nil {\nlog.Fatalf(\"client did not initialize in time: %s\", err)\n}\n```\n\n### Code Sample\nThe following is a complete code example that you can use to test the `harnessappdemodarkmode` Flag you created on the Harness Platform. When you run the code it will:\n- Connect to the FF service.\n- Report the value of the Flag every 10 seconds until the connection is closed. Every time the harnessappdemodarkmode Flag is toggled on or off on the Harness Platform, the updated value is reported. \n- Close the SDK.\n\n\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"os\"\n\t\"time\"\n\n\tharness \"github.com/harness/ff-golang-server-sdk/client\"\n\t\"github.com/harness/ff-golang-server-sdk/evaluation\"\n)\n\nvar (\n\tflagName string = getEnvOrDefault(\"FF_FLAG_NAME\", \"harnessappdemodarkmode\")\n\tapiKey   string = getEnvOrDefault(\"FF_API_KEY\", \"changeme\")\n)\n\nfunc main() {\n\tlog.Println(\"Harness SDK Getting Started\")\n\n\t// Create a feature flag client\n\tclient, err := harness.NewCfClient(apiKey)\n\tif err != nil {\n\t\tlog.Fatalf(\"could not connect to CF servers %s\\n\", err)\n\t}\n\tdefer func() { client.Close() }()\n\n\t// Create a target (different targets can get different results based on rules)\n\ttarget := evaluation.Target{\n\t\tIdentifier: \"golangsdk\",\n\t\tName:       \"GolangSDK\",\n\t\tAttributes: \u0026map[string]interface{}{\"location\": \"emea\"},\n\t}\n\n\t// Loop forever reporting the state of the flag\n\tfor {\n\t\tresultBool, err := client.BoolVariation(flagName, \u0026target, false)\n\t\tif err != nil {\n\t\t\tlog.Fatal(\"failed to get evaluation: \", err)\n\t\t}\n\t\tlog.Printf(\"Flag variation %v\\n\", resultBool)\n\t\ttime.Sleep(10 * time.Second)\n\t}\n\n}\n\nfunc getEnvOrDefault(key, defaultStr string) string {\n\tvalue := os.Getenv(key)\n\tif value == \"\" {\n\t\treturn defaultStr\n\t}\n\treturn value\n}\n```\n\n### Running the example\n\n```bash\nexport FF_API_KEY=\u003cyour key here\u003e\ngo run examples/getting_started.go\n```\n\n### Running the example with Docker\nIf you dont have the right version of golang installed locally, or don't want to install the dependencies you can\nuse docker to quick get started\n\n```bash \nexport FF_API_KEY=\u003cyour key here\u003e\ndocker run -e FF_API_KEY=$FF_API_KEY -v $(pwd):/app -w /app golang:1.17 go run examples/getting_started.go\n```\n\n\n\n### Additional Reading\n\nFor further examples and config options, see the [Golang SDK Reference](https://ngdocs.harness.io/article/4c8wljx60w-feature-flag-sdks-go-application).\n\nFor more information about Feature Flags, see our [Feature Flags documentation](https://ngdocs.harness.io/article/0a2u2ppp8s-getting-started-with-feature-flags).\n\n-------------------------\n[Harness](https://www.harness.io/) is a feature management platform that helps teams to build better software and to\ntest features quicker.\n\n-------------------------\n\n### Code Cleanup (Beta)\nThe go sdk supports automated code cleanup. For more info see the [docs](/examples/code_cleanup/README.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharness%2Fff-golang-server-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharness%2Fff-golang-server-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharness%2Fff-golang-server-sdk/lists"}