Feature Flag Determination

Feature flags can have two states: turned on (ON) and turned off (OFF). Different features are configured based on each state. When a user accesses a feature with a feature flag applied, they should receive the ON or OFF state. This state determination can be done through the Hackle SDK.

IsFeatureOn

By passing the feature key to the isFeatureOn() method, you can receive the state result for the user. Then, you implement the logic based on the state.

In the example code below, we are passing feature key 42, and the user's user identifier is "ae03e1adf."

import "github.com/hackle-io/hackle-go-sdk/hackle"

// Determine the user's state in the feature flag with feature key 42.
// If no decision can be made, it returns false (OFF state) by default.
user := hackle.NewUserBuilder().ID("ae2182e0").Build()
isFeatureOn := hackleClient.IsFeatureOn(42, user)

if isFeatureOn {
    // Logic for the ON state
} else {
    // Logic for the OFF state
}

featureFlagDetail

The featureFlagDetail() method works the same as the isFeatureOn() method and additionally provides a reason for determining the status of a particular user. This can be useful when you want to check whether the manual allocation is working well or when you think that the traffic allocation you have set is not being reflected in your results.

You must pass a feature key as a parameter. For the example code below, we are passing feature key 42.

import "github.com/hackle-io/hackle-go-sdk/hackle"

user := hackle.NewUserBuilder().ID("ae2182e0").Build()
decision := hackleClient.FeatureFlagDetail(42, user)

// Check if the feature is on or off
isFeatureOn := decision.IsOn()

// State determination reason
reason := decision.Reason()

The state determination reason is received in a format like SDK_NOT_READY. You can refer to the table below for more details:

ReasonDescriptionDistribution Result
SDK_NOT_READYThe SDK is not ready for use.
(e.g., attempting initialization with an incorrect SDK key)
Default state (OFF)
FEATURE_FLAG_NOT_FOUNDThe feature flag for the provided feature key cannot be found.
The feature key may be incorrect or the feature could be in a storage state.
Default state (OFF)
FEATURE_FLAG_INACTIVEThe feature flag is in the OFF state.Default state (OFF)
INDIVIDUAL_TARGET_MATCHMatched for individual targeting.State set for individual targeting
TARGET_RULE_MATCHMatched for user targeting.State set for user targeting
DEFAULT_RULENot matched for individual targeting or user targeting.State set as default rule
EXCEPTIONAn unknown error has occurred.Default state (OFF)

Feature Flag Parameters

You can also receive the parameter values for the feature flag's state determination using the FeatureFlagDetail() method. Since Hackle's feature flag settings often include parameters in the form of key-value pairs, you can use the following methods to access and utilize these parameter values based on their types:

GetString

Returns the parameter value set as STRING or JSON in the feature flag's configuration.

import "github.com/hackle-io/hackle-go-sdk/hackle"

user := hackle.NewUserBuilder().ID("ae2182e0").Build()
decision := hackleClient.FeatureFlagDetail(42, user)
  
stringValue := decision.GetString("string_key", "default_value")
jsonValue := decision.GetString("json_key", "{}")

GetNumber

Returns the parameter value set as a Number type as a float64.

import "github.com/hackle-io/hackle-go-sdk/hackle"

user := hackle.NewUserBuilder().ID("ae2182e0").Build()
decision := hackleClient.FeatureFlagDetail(42, user)
  
numberValue := decision.GetNumber("number_key", 42.0)

GetBool

Returns the parameter value set as a boolean type.

import "github.com/hackle-io/hackle-go-sdk/hackle"

user := hackle.NewUserBuilder().ID("ae2182e0").Build()
decision := hackleClient.FeatureFlagDetail(42, user)
  
boolValue := decision.GetBool("bool_key", false)