title | description |
---|---|
Go SDK and Go Gin |
Getting Started with the OpenFeature Go SDK and Go Gin |
import FlagdContent from '@site/src/components/custom/tutorial/flagd-content.mdx'; import FlagdChangeContent from '@site/src/components/custom/tutorial/flagd-change-content.mdx'; import WhyDefaultContent from '@site/src/components/custom/tutorial/why-default-content.mdx';
This walk-through teaches you the basics of using OpenFeature in Go in the context of a Go Gin web application.
You'll learn how to:
- Integrate the OpenFeature Go SDK
- Install and configure the OpenFeature provider
- Perform basic feature flagging
This walk-through assumes that:
- You have a basic knowledge of Go
- You have Go 1.17 or later
- You have Docker installed and running on the host system
To get started, let's create a new folder and initialize the project. This can be done by running following commands.
mkdir gostart
cd gostart
go mod init gostart
Let's initialize the application with logic to expose a minimal Go Gin web endpoint.
First, create the file main.go
and include the following code.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
const defaultMessage = "Hello!"
func main() {
// Initialize Go Gin
engine := gin.Default()
// Setup a simple endpoint
engine.GET("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, defaultMessage)
})
engine.Run()
}
To add Go Gin dependency to the application, run the following commands.
go get github.com/gin-gonic/gin
go mod tidy
Next, let's add logic to customize the response message based on the boolean
flag key welcome-message
.
package main
import (
// diff-add
"context"
"github.com/gin-gonic/gin"
// diff-add
"github.com/open-feature/go-sdk/openfeature"
"net/http"
)
const defaultMessage = "Hello!"
// diff-add
const newWelcomeMessage = "Hello, welcome to this OpenFeature-enabled website!"
func main() {
// diff-add-block-start
// Initialize OpenFeature client
client := openfeature.NewClient("GoStartApp")
// diff-add-block-end
// Initialize Go Gin
engine := gin.Default()
// Setup a simple endpoint
engine.GET("/hello", func(c *gin.Context) {
// diff-remove-block-start
c.JSON(http.StatusOK, defaultMessage)
// diff-remove-block-end
// diff-add-block-start
// Evaluate welcome-message feature flag
welcomeMessage, _ := client.BooleanValue(
context.Background(), "welcome-message", false, openfeature.EvaluationContext{},
)
if welcomeMessage {
c.JSON(http.StatusOK, newWelcomeMessage)
return
} else {
c.JSON(http.StatusOK, defaultMessage)
return
}
// diff-add-block-end
})
engine.Run()
}
To add OpenFeature SDK dependency to the application, run the following commands.
go get github.com/open-feature/go-sdk/openfeature
go mod tidy
Let's start the application and see it in action.
go run main.go
Now you can visit the url http://localhost:8080/hello and observe the message Hello!.
Now, let's make the required code changes in our application.
import (
"context"
"github.com/gin-gonic/gin"
// diff-add
flagd "github.com/open-feature/go-sdk-contrib/providers/flagd/pkg"
"log"
// diff-add-block-end
"github.com/open-feature/go-sdk/openfeature"
"net/http"
)
func main() {
// diff-add-block-start
// Use flagd as the OpenFeature provider
err := openfeature.SetProviderAndWait(flagd.NewProvider())
if err != nil {
// If a provider initialization error occurs, log it and exit
log.Fatalf("Failed to set the OpenFeature provider: %v", err)
}
// diff-add-block-end
// Initialize OpenFeature client
client := openfeature.NewClient("GoStartApp")
Then, let's add flagd provider
dependency with following commands.
go get github.com/open-feature/go-sdk-contrib/providers/flagd
go mod tidy
The complete main.go
file is given below:
package main
import (
"context"
"log"
"net/http"
"github.com/gin-gonic/gin"
flagd "github.com/open-feature/go-sdk-contrib/providers/flagd/pkg"
"github.com/open-feature/go-sdk/openfeature"
)
const defaultMessage = "Hello!"
const newWelcomeMessage = "Hello, welcome to this OpenFeature-enabled website!"
func main() {
// Use flagd as the OpenFeature provider
err := openfeature.SetProviderAndWait(flagd.NewProvider())
if err != nil {
// If an error occurs, log it and exit
log.Fatalf("Failed to set the OpenFeature provider: %v", err)
}
// Initialize OpenFeature client
client := openfeature.NewClient("GoStartApp")
// Initialize Go Gin
engine := gin.Default()
// Setup a simple endpoint
engine.GET("/hello", func(c *gin.Context) {
// Evaluate welcome-message feature flag
welcomeMessage, _ := client.BooleanValue(
context.Background(), "welcome-message", false, openfeature.EvaluationContext{},
)
if welcomeMessage {
c.JSON(http.StatusOK, newWelcomeMessage)
return
} else {
c.JSON(http.StatusOK, defaultMessage)
return
}
})
engine.Run()
}
Let's rerun the application with the following command.
go run main.go
Revisit the endpoint http://localhost:8080/hello and you will see the same value.
Revisit the endpoint http://localhost:8080/hello and you will be greeted with Hello, welcome to this OpenFeature-enabled website!
This walk-through introduced you to the OpenFeature Go SDK and how it can be easily integrated into well-known frameworks such as Go Gin. It covered how a provider can be configured to perform the flag evaluation and introduced basic feature flagging concepts. It also showcased how feature flags can be updated at runtime, without requiring a code change and a redeployment.