This document guides you through setting up the "Cosmic Coffee Co." demo application and the required configuration in your Flagsmith project.
- Go (version 1.18 or later) installed on your machine.
- A Flagsmith account. If you don't have one, you can sign up for free.
First, we need to create the flags and segments in your Flagsmith project that the Go application will use.
- Log in to your Flagsmith account.
- Create a new project named "Cosmic Coffee Co." or similar.
- Navigate to the Settings page for your project's Development environment and copy the Server-Side Environment Key. You will need this key to run the app.
Navigate to the Features page and create the following three flags:
-
Dark Mode Toggle (
dark_mode)- ID:
dark_mode - Type: Standard Feature Flag (On/Off)
- Enabled by default: You can set this to either on or off to start.
- ID:
-
Sale Banner & Discount (
sale_banner)- ID:
sale_banner - Type: Remote Config (Value-based flag)
- Enabled by default: Check this on.
- Value: Set the value to 10%.
- ID:
-
Beta Tester Banner (
beta_user_banner)- ID:
beta_user_banner - Type: Standard Feature Flag (On/Off)
- Enabled by default: Leave this unchecked (disabled). We will enable it for a specific segment only.
- ID:
Now, let's create a segment to target our internal beta testers.
- Navigate to the Segments page on the left-hand navigation.
- Click Create a Segment.
- Name the segment Beta Testers.
- Add a rule:
- Trait Key:
is_beta_tester - Condition: Equal to
- Value: true
- Trait Key:
- Click Create Segment.
Finally, we'll use the segment to turn on the beta_user_banner only for our beta testers.
- Go back to the Features page.
- Find the
beta_user_bannerflag and click on it. - Go to the Overrides tab.
- Click Add Segment Override.
- Select the Beta Testers segment.
- In the override rule, toggle the feature ON.
- Click Create Override.
Now, the beta_user_banner is disabled for everyone except users who are identified as being part of the "Beta Testers" segment.
- Save the Code: Save the Go code from the first file as
main.go. - Set Environment Key: Open your terminal and set the environment variable for your Flagsmith key.
export FLAGSMITH_ENVIRONMENT_KEY="<your_flagsmith_server_side_key>"Replace <your_flagsmith_server_side_key> with the key you copied in Step A.
- Install Dependencies: In the same directory as
main.go, run the following commands to initialize a Go module and fetch the Flagsmith client.
go mod init cosmic-coffee-demo
go get github.com/Flagsmith/flagsmith-go-client/v3- Run the App:
go run main.goYou should see a message saying Cosmic Coffee Co. server starting on http://localhost:8080.
Now for the fun part!
- Default View: Open your browser and go to http://localhost:8080. You'll see the standard page.
- Become a Beta Tester: Now, go to http://localhost:8080?user=beta_tester. You should see the purple "You are a Beta Tester!" banner appear at the top! This is the segment override in action.
- Control from Flagsmith:
- Go to your Flagsmith dashboard.
- Toggle the
dark_modeflag on and off. Refresh your browser page to see the site switch between light and dark mode instantly. - Change the value of the
sale_bannerfrom 10% to 25% OFF. Refresh your browser—the text in the green sale banner will update immediately.
You've now successfully controlled your running Go application from an external dashboard without touching a single line of code!