Skip to main content

Creating a Hook for the go-sdk

· 3 min read
Skye Gill

A Hook taps into one or more of the flag evaluation's lifecycle events (before/after/error/finally) to perform the same action at that point for every evaluation. In this blog post, we'll look into what it takes to create an OpenFeature hook in Go that verifies that the return value is a valid hex color.


Create and integrate a spec compliant hook that validates that the result of a flag evaluation is a hex color.


  • Golang 1.17+

Repository setup

Hooks written for the go-sdk are all maintained in the go-sdk-contrib repository, containing both hooks and providers. The following commands can be used to setup the go-sdk-contrib repository, this clones the repository and sets up your hook specific go module under /hooks/MY-NEW-HOOK-NAME adding a go.mod and file. The module will then be referenced in the top level file.

git clone
cd go-sdk-contrib
make HOOK=MY-NEW-HOOK-NAME new-hook
make workspace-init

Creating the hook

In order for the Hook to be compatible with the OpenFeature go-sdk, it will need to comply to the OpenFeature spec. For the go-sdk this means that it will need to conform to the following interface:

type Hook interface {
Before(ctx context.Context, hookContext HookContext, hookHints HookHints) (*EvaluationContext, error)
After(ctx context.Context, hookContext HookContext, flagEvaluationDetails InterfaceEvaluationDetails, hookHints HookHints) error
Error(ctx context.Context, hookContext HookContext, err error, hookHints HookHints)
Finally(ctx context.Context, hookContext HookContext, hookHints HookHints)

In order to avoid implementing all the required functions to conform to the interface, embed the of.UnimplementedHook struct in the Hook struct. Then define the After lifecycle event, overriding of.UnimplementedHook's null implementation.

// Hook validates the flag evaluation details After flag resolution
type Hook struct {
Validator validator

func (h Hook) After(ctx context.Context, hookContext of.HookContext, flagEvaluationDetails of.InterfaceEvaluationDetails, hookHints of.HookHints) error {
err := h.Validator.IsValid(flagEvaluationDetails)
if err != nil {
return err

return nil

Notice the Validator field of type validator in the Hook struct. This is defined as such:

type validator interface {
IsValid(flagEvaluationDetails of.InterfaceEvaluationDetails) error

This allows us to supply any validator that implements this function signature, you can either create your own validator or use one of the existing validators. This tutorial uses the existing hex regex validator.

Integrating the hook

  1. Install dependencies

    go get
    go get
  2. Import the dependencies

    package main

    import (
  3. Create an instance of the validator hook struct using the regex hex validator

    func main() {
    hexValidator, err := regex.Hex()
    if err != nil {
    v := validator.Hook{Validator: hexValidator}
  4. Register the NoopProvider, this simply returns the given default value on flag evaluation.

    This step is optional, the sdk uses the NoopProvider by default but we're explicitly setting it for completeness

  5. Create the client, call the flag evaluation using the validator hook at the point of invocation

    client := openfeature.NewClient("foo")

    result, err := client.
    if err != nil {
    fmt.Println("err:", err)
    fmt.Println("result:", result)
  6. Check that the flag evaluation returns an error as invalidhex is not a valid hex color

    go run main.go
    err: execute after hook: regex doesn't match on flag value
    result {blue 1 {invalidhex }}

    Note that despite getting an error we still get a result.