Go program pattern 05: Decorations

Go is a statically typed compiled language designed to be concise and efficient. While Go is not a purely object-oriented language, we can still use design patterns to improve code readability and maintainability. Today, I will introduce a common design pattern: the Decorator pattern.

This article is first published in the medium MPP plan. If you are a medium user, please follow me in medium. Thank you very much.

What is the Decorator Pattern?

The Decorator pattern is a design pattern that allows us to dynamically add behavior to an object at runtime without altering its implementation. This is achieved by creating a wrapper object or decorator that contains the original object and provides an enhanced interface to add new behavior.

In Go, we can use functions as decorators because Go supports higher-order functions, which means functions can be passed as parameters and returned as values.

An Example

To better understand the Decorator pattern, let’s see how we can implement it in Go through an example.

First, we define a function type Foo and a decorator type FooDecorator:

1
2
3
type Foo func(string) string

type FooDecorator func(Foo) Foo

Then, we can create a decorator that takes a function of type Foo and returns a new function of type Foo which adds some behavior before and after calling the original function:

1
2
3
4
5
6
7
8
func WithLog(decorated Foo) Foo {
return func(s string) string {
fmt.Println("Before calling the decorated function")
result := decorated(s)
fmt.Println("After calling the decorated function")
return result
}
}

Now, we can create a Foo function and enhance it using the decorator:

1
2
3
4
5
6
7
8
9
10
func main() {
foo := func(s string) string {
fmt.Println("Foo function called")
return s
}

foo = WithLog(foo)

foo("Hello, world!")
}

In this example, we create a Foo function and use the WithLog decorator to enhance it. When we call the enhanced function, it first prints a message, then calls the original Foo function, and finally prints another message.

This is the Decorator pattern in Go. By using decorators, we can dynamically add new behavior without modifying the original function.

Next, let’s look at an example related to handling HTTP requests. First, we’ll start with a simple HTTP server code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithServerHeader()")
w.Header().Set("Server", "HelloServer v0.0.1")
h(w, r)
}
}

func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Received Request %s from %s\n", r.URL.Path, r.RemoteAddr)
fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}

func main() {
http.HandleFunc("/v1/hello", WithServerHeader(hello))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

In this code, we use the Decorator pattern. The WithServerHeader() function acts as a decorator that takes an http.HandlerFunc and returns a modified version. This example is relatively simple, as we only add a response header using WithServerHeader(). However, we can create many more functions like this, such as writing authentication cookies, checking authentication cookies, and logging.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
"fmt"
"log"
"net/http"
"strings"
)

func WithServerHeader(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithServerHeader()")
w.Header().Set("Server", "HelloServer v0.0.1")
h(w, r)
}
}

func WithAuthCookie(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithAuthCookie()")
cookie := &http.Cookie{Name: "Auth", Value: "Pass", Path: "/"}
http.SetCookie(w, cookie)
h(w, r)
}
}

func WithBasicAuth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithBasicAuth()")
cookie, err := r.Cookie("Auth")
if err != nil || cookie.Value != "Pass" {
w.WriteHeader(http.StatusForbidden)
return
}
h(w, r)
}
}

func WithDebugLog(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("--->WithDebugLog")
r.ParseForm()
log.Println(r.Form)
log.Println("path", r.URL.Path)
log.Println("scheme", r.URL.Scheme)
log.Println(r.Form["url_long"])
for k, v := range r.Form {
log.Println("key:", k)
log.Println("val:", strings.Join(v, ""))
}
h(w, r)
}
}

func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Received Request %s from %s\n", r.URL.Path, r.RemoteAddr)
fmt.Fprintf(w, "Hello, World! "+r.URL.Path)
}

func main() {
http.HandleFunc("/v1/hello", WithServerHeader(WithAuthCookie(hello)))
http.HandleFunc("/v2/hello", WithServerHeader(WithBasicAuth(hello)))
http.HandleFunc("/v3/hello", WithServerHeader(WithBasicAuth(WithDebugLog(hello))))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

Pipeline of Multiple Decorators

When using multiple decorators, the code can become less visually appealing as we need to nest functions layer by layer. However, we can refactor the code to make it cleaner. To do this, we first write a utility function that iterates through and calls each decorator:

1
2
3
4
5
6
7
8
9
type HttpHandlerDecorator func(http.HandlerFunc) http.HandlerFunc

func Handler(h http.HandlerFunc, decors ...HttpHandlerDecorator) http.HandlerFunc {
for i := range decors {
d := decors[len(decors)-1-i] // iterate in reverse
h = d(h)
}
return h
}

Then, we can use it like this:

1
2
http.HandleFunc("/v4/hello", Handler(hello,
WithServerHeader, WithBasicAuth, WithDebugLog))

Conclusion

In this article, I demonstrated the Decorator pattern using two examples. However, since Go does not support annotations as a syntactic sugar, using decorators can be a bit cumbersome. Nevertheless, the concept is still important, and we can apply this way of thinking to write higher-quality code in our daily development.