{"id":18853042,"url":"https://github.com/featbit/featbit-go-sdk","last_synced_at":"2026-01-08T16:06:12.952Z","repository":{"id":68957000,"uuid":"580756555","full_name":"featbit/featbit-go-sdk","owner":"featbit","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-21T08:03:06.000Z","size":83,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"v1","last_synced_at":"2025-02-05T07:29:51.582Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/featbit.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,"publiccode":null,"codemeta":null}},"created_at":"2022-12-21T11:19:38.000Z","updated_at":"2023-10-08T14:42:01.000Z","dependencies_parsed_at":"2023-12-21T10:21:34.919Z","dependency_job_id":"c9f82f98-bb17-4cca-880d-599cdbb552ed","html_url":"https://github.com/featbit/featbit-go-sdk","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"ddd6a9d2273b3661a48af5b72dc083bda73bf7aa"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-go-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-go-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-go-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-go-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/featbit","download_url":"https://codeload.github.com/featbit/featbit-go-sdk/tar.gz/refs/heads/v1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246278817,"owners_count":20751834,"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":[],"created_at":"2024-11-08T03:42:45.246Z","updated_at":"2026-01-08T16:06:12.921Z","avatar_url":"https://github.com/featbit.png","language":"Go","readme":"# FeatBit Server-Side SDK for Go\n\n## Introduction\n\nThis is the Go Server-Side SDK for the 100% open-source feature flags management\nplatform [FeatBit](https://github.com/featbit/featbit). \n\nThe FeatBit Server-Side SDK for Go is designed primarily for use in multi-user systems such as web servers and\napplications.\n\n## Data synchronization\n\nWe use websocket to make the local data synchronized with the FeatBit server, and then store them in memory by\ndefault. Whenever there is any change to a feature flag or its related data, this change will be pushed to the SDK and\nthe average synchronization time is less than 100 ms. Be aware the websocket connection may be interrupted due to\ninternet outage, but it will be resumed automatically once the problem is gone.\n\nIf you want to use your own data source, see [Offline Mode](#offline-mode).\n\n## Get Started\n\nGo Server Side SDK is based on go 1.13, so you need to install go 1.13 or above.\n\n### Installation\n\n```\ngo get github.com/featbit/featbit-go-sdk\n```\n\n### Prerequisite\n\nBefore using the SDK, you need to obtain the environment secret and SDK URLs. \n\nFollow the documentation below to retrieve these values\n\n- [How to get the environment secret](https://docs.featbit.co/sdk/faq#how-to-get-the-environment-secret)\n- [How to get the SDK URLs](https://docs.featbit.co/sdk/faq#how-to-get-the-sdk-urls)\n  \n### Quick Start\n\u003e Note that the _**envSecret**_, _**streamUrl**_ and _**eventUrl**_ are required to initialize the SDK.\n\nThe following code demonstrates basic usage of the SDK.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/featbit/featbit-go-sdk\"\n\t\"github.com/featbit/featbit-go-sdk/interfaces\"\n)\n\nfunc main() {\n\tenvSecret := \"\u003creplace-with-your-env-secret\u003e\"\n\tstreamingUrl := \"ws://localhost:5100\"\n\teventUrl := \"http://localhost:5100\"\n\n\tclient, err := featbit.NewFBClient(envSecret, streamingUrl, eventUrl)\n\n\tdefer func() {\n\t\tif client != nil {\n\t\t\t// ensure that the SDK shuts down cleanly and has a chance to deliver events to FeatBit before the program exits\n\t\t\t_ = client.Close()\n\t\t}\n\t}()\n\n\tif err == nil \u0026\u0026 client.IsInitialized() {\n\t\tuser, _ := interfaces.NewUserBuilder(\"\u003creplace-with-your-user-key\u003e\").UserName(\"\u003creplace-with-your-user-name\u003e\").Build()\n\t\t_, ed, _ := client.BoolVariation(\"\u003creplace-with-your-feature-flag-key\u003e\", user, false)\n\t\tfmt.Printf(\"flag %s, returns %s for user %s, reason: %s \\n\", ed.KeyName, ed.Variation, user.GetKey(), ed.Reason)\n\t} else {\n\t\tfmt.Println(\"SDK initialization failed\")\n    }\n}\n```\n\n### Examples\n\n- [Go Demo](https://github.com/featbit/featbit-samples/blob/main/samples/dino-game/demo-golang/go_demo.go)\n\n### FBClient\n\nApplications **SHOULD instantiate a single FBClient instance** for the lifetime of the application. In the case where an application\nneeds to evaluate feature flags from different environments, you may create multiple clients, but they should still be\nretained for the lifetime of the application rather than created per request or per thread.\n\n#### Bootstrapping\n\nThe bootstrapping is in fact the call of constructor of `featbit.FBClient`, in which the SDK will be initialized, using\nstreaming from your feature management platform.\n\nThe constructor will return when it successfully connects, or when the timeout set\nby `featbit.FBConfig.StartWait`(default: 15 seconds) expires, whichever comes first. If it has not succeeded in connecting when the timeout elapses,\nyou will receive the client in an uninitialized state where feature flags will return default values; it will still\ncontinue trying to connect in the background unless there has been an `net.DNSError` or you close the client. \nYou can detect whether initialization has succeeded by calling `featbit.FBClient.IsInitialized()`.\n\nIf `featbit.FBClient.IsInitialized()` returns True, it means the `featbit.FBClient` has succeeded at some point in connecting to feature flag center, \notherwise client has not yet connected to feature flag center, or has permanently failed. In this state, feature flag evaluations will always return default values.\n\n```go\nconfig := featbit.FBConfig{StartWait: 10 * time.Second}\n// DO NOT forget to close the client when you don't need it anymore\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)\nif err == nil \u0026\u0026 client.IsInitialized() {\n    // the client is ready\n}\n\n```\n\nIf you prefer to have the constructor return immediately, and then wait for initialization to finish at some other\npoint, you can use `featbit.FBClient.GetDataUpdateStatusProvider()`, which will return an implementation of `interfaces.DataUpdateStatusProvider`.\nThis interface has a `WaitForOKState` method that will block until the client has successfully connected, or until the timeout expires.\n\n```go\nconfig := featbit.FBConfig{StartWait: 0}\n// DO NOT forget to close the client when you don't need it anymore\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)\nif err != nil {\n    return\n}\nok := client.GetDataSourceStatusProvider().WaitForOKState(10 * time.Second)\nif ok {\n    // the client is ready\n}\n```\n\u003e To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.\n\n\n### FBConfig and Components\n\nIn most cases, you don't need to care about `featbit.FBConfig` and the internal components, just initialize SDK like:\n\n```go\nclient, err := featbit.NewFBClient(envSecret, streamingUrl, eventUrl)\n```\n\n`envSecret` _**sdkKey(envSecret)**_ is id of your project in FeatBit feature flag center\n\n`streamingURL`: URL of your feature management platform to synchronize feature flags, user segments, etc.\n\n`eventURL`: URL of your feature management platform to send analytics events\n\n`StartWait`: how long the constructor will block awaiting a successful data sync. Setting this to a zero or negative\nduration will not block and cause the constructor to return immediately.\n\n`Offline`: Set whether SDK is offline. when set to true no connection to your feature management platform anymore\n\n`featbit.FBConfig` provides advanced configuration options for setting the SDK component, or you want to customize the behavior\nof build-in components.\n\n`NetworkFactory`: sets the SDK networking configuration, _**DO NOT**_ change it unless you should set some advanced configuration such as\nHTTP Proxy, TLS etc.\n\n`factories.NetworkBuilder` is the default `NetworkFactory`\n\n```go\nfactory := factories.NewNetworkBuilder()\nfactory.ProxyUrl(\"http://username:password@146.137.9.45:65233\")\n\nconfig := featbit.DefaultFBConfig\nconfig.NetworkFactory = factory\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, *config)\n// or\nconfig := featbit.FBConfig{NetworkFactory: factory}\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)\n```\n\n`DataStorageFactory` sets the implementation of `interfaces.DataStorage` to be used for holding feature flags and\nrelated data received from feature flag center SDK sets the implementation of the data storage, in using `factories.InMemoryStorageBuilder` by default\nto instantiate a memory data storage. Developers can customize the data storage to persist received data in redis,\nmongodb, etc.\n\n`DataSynchronizerFactory` SDK sets the implementation of the `interfaces.DataSynchronizer` that receives feature flag data\nfrom feature flag center, in using `factories.StreamingBuilder` by default\nIf Developers would like to know what the implementation is, they can read the GoDoc and source code.\n\n`InsightProcessorFactory` SDK which sets the implementation of `interfaces.InsightProcessor` to be used for processing analytics events.\nusing a factory object. The default is `factories.InsightProcessorBuilder`.\nIf Developers would like to know what the implementation is, they can read the GoDoc and source code.\n\nIt's not recommended to change the default factories in the `featbit.FBConfig`\n\n### FBUser\n\nA collection of attributes that can affect flag evaluation, usually corresponding to a user of your\napplication.\nThis object contains built-in properties(`key`, `userName`). The `key` and `userName` are required.\nThe `key` must uniquely identify each user; this could be a username or email address for authenticated users, or an ID\nfor anonymous users.\nThe `userName` is used to search your user quickly.\nYou may also define custom properties with arbitrary names and values.\n\n```go\n// FBUser creation\nuser, err := NewUserBuilder(\"key\").UserName(\"name\").Custom(\"property\", \"value\").Build()\n```\n\n### Evaluation\n\nSDK calculates the value of a feature flag for a given user, and returns a flag value and `interfaces.EvalDetail` that describes the way\nthat the value was determined.\n\nSDK will initialize all the related data(feature flags, segments etc.) in the bootstrapping and receive the data updates\nin real time, as mentioned in [Bootstrapping](#bootstrapping).\n\nAfter initialization, the SDK has all the feature flags in the memory and all evaluation is done _**locally and\nsynchronously**_, the average evaluation time is \u003c _**10**_ ms.\n\nSDK supports String, Boolean, and Number and Json as the return type of flag values:\n\n- Variation(for string)\n- BoolVariation\n- IntVariation\n- DoubleVariation\n- JsonVariation\n\n```go\n// be sure that SDK is initialized before evaluation\n// DO not forget to close client when you are done with it\nif client.isInitialized() {\n    // Flag value\n    // returns a string variation\n    variation, detail, _ := client.Variation(\"flag key\", user, \"Not Found\")\n}\n```\n\n`featbit.FBClient.AllLatestFlagsVariations(user)` returns all variations for a given user. You can retrieve the flag value or details\nfor a specific flag key:\n\n- GetStringVariation\n- GetBoolVariation\n- GetIntVariation\n- GetDoubleVariation\n- GetJsonVariation\n\n```go\n// be sure that SDK is initialized before evaluation\n// DO not forget to close client when you are done with it\nif client.isInitialized() {\n    // get all variations for a given user in your project \n    allState, _ := client.AllLatestFlagsVariations(user)\n    variation, detail, _ := allState.GetStringVariation(\"flag key\", \"Not Found\")\n}\n```\n\n\u003e Note that if evaluation called before Go SDK client initialized, you set the wrong flag key/user for the evaluation or the related feature flag\nis not found, SDK will return the default value you set. `interfaces.EvalDetail` will explain the details of the latest evaluation including error raison.\n\n### Offline Mode\n\nIn some situations, you might want to stop making remote calls to FeatBit. Here is how:\n\n```go\nconfig := featbit.DefaultFBConfig\nfeatbit.Offline = true\nfeatbit.StartWait = 1 * time.Millisecond\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, *config)\n// or\nconfig := FBConfig{Offline: true, StartWait: 1 * time.Millisecond}\nclient, err := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)\n\n```\n\nWhen you put the SDK in offline mode, no insight message is sent to the server and all feature flag evaluations return\nfallback values because there are no feature flags or segments available. If you want to use your own data source,\nSDK allows users to populate feature flags and segments data from a JSON string. Here is an example: [fbclient_test_data.json](fixtures/fbclient_test_data.json).\n\nThe format of the data in flags and segments is defined by FeatBit and is subject to change. Rather than trying to\nconstruct these objects yourself, it's simpler to request existing flags directly from the FeatBit server in JSON format\nand use this output as the starting point for your file. Here's how:\n\n```shell\n# replace http://localhost:5100 with your evaluation server url\ncurl -H \"Authorization: \u003cyour-env-secret\u003e\" http://localhost:5100/api/public/sdk/server/latest-all \u003e featbit-bootstrap.json\n```\n\nThen you can use this file to initialize the SDK in offline mode:\n\n```go\n// first load data from file and then \nok, _ := client.InitializeFromExternalJson(string(jsonBytes))\n```\n\n### Experiments (A/B/n Testing)\n\nWe support automatic experiments for page-views and clicks, you just need to set your experiment on FeatBit platform,\nthen you should be able to see the result in near real time after the experiment is started.\n\nIn case you need more control over the experiment data sent to our server, we offer a method to send custom event.\n\n```go\n// for the percentage experiment\nclient.TrackPercentageMetric(user, eventName)\n// for the numeric experiment\nclient.TrackNumericMetric(user, eventName, numericValue)\n```\n\nMake sure `featbit.FBClient.TrackPercentageMetric()` or `featbit.FBClient.TrackNumericMetric()`  is called after the related feature flag is called,\notherwise the custom event may not be included into the experiment result.\n\n\n## Getting support\n\n- If you have a specific question about using this sdk, we encourage you\n  to [ask it in our slack](https://join.slack.com/t/featbit/shared_invite/zt-1ew5e2vbb-x6Apan1xZOaYMnFzqZkGNQ).\n- If you encounter a bug or would like to request a\n  feature, [submit an issue](https://github.com/featbit/featbit-go-sdk/issues/new).\n\n## See Also\n- [Connect To Go Sdk](https://docs.featbit.co/sdk/overview#go)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeatbit%2Ffeatbit-go-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeatbit%2Ffeatbit-go-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeatbit%2Ffeatbit-go-sdk/lists"}