Strategy Pattern in Golang


The strategy is a behavior pattern. It fits where multiple choices for an interface are available and the design would allow switching them dynamically.


  • A set of choices with similarity in the interface
  • Dynamic update of a choice


  1. A cache with evictions
    • Multiple eviction algorithms
    • Cache could switch to a different algorithm dynamically
  2. A duck type has a variety of flying behaviors. A duck can fly very fast, slow and no-fly.


Define what a duck is.

// duck_type.go

package main

type duck struct {

Define fly behavior

// fly_behavior.go

package main

type flyBehavior interface {

Define the fast duck fly behavior


package main

import "fmt"

type duckF18 struct {

func (d *duckF18) fly() {
	fmt.Printf("%+v\n", "flying like a F18")

func (d *duckF18) nofly() {
	fmt.Printf("%+v\n", "not implemented")

Define no fly behavior

// nofly_duck.go

package main

import "fmt"

type duckNofly struct {

func (d *duckNofly) fly() {
	fmt.Printf("%+v\n", "not implemented")

func (d *duckNofly) nofly() {
	fmt.Printf("%+v\n", "no flying")

// main.go

package main

func createNewDuck(f flyBehavior) *duck {
	return &duck{
		flyBehavior: f,

func (d *duck) setFlyBehavior(f flyBehavior) {
	d.flyBehavior = f

func main() {
	// f18 duck
	aDuck := createNewDuck(&duckF18{})

    // duck switches to no fly



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: