Skip to content

Instantly share code, notes, and snippets.

@stevenferrer
Created August 29, 2017 18:04
Show Gist options
  • Save stevenferrer/e4c1eb973a930c2205039448cda6d3d9 to your computer and use it in GitHub Desktop.
Save stevenferrer/e4c1eb973a930c2205039448cda6d3d9 to your computer and use it in GitHub Desktop.
Demonstrating the decorator pattern
package main
import (
"fmt"
"strings"
)
type Decorator func(string) string
func Upper() Decorator {
return func(s string) string {
return strings.ToUpper(s)
}
}
func Prefix(prefix string) Decorator {
return func(s string) string {
return prefix + s
}
}
func main() {
decorators := []Decorator{
Upper(),
Prefix("MyPrefix"),
}
s := "-TheString"
for _, dc := range decorators {
s = dc(s)
}
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment