title: Decorator functions in Go
body: <p>Decorator pattern (functions) has <a href="https://github.com/smaxwellstewart/golang-decorator-pattern" rel="noreferrer">many benefits</a>:</p>
  <blockquote>
  <p>It is very useful when a method has many orthogonal concerns... I.e., None of these concerns are related, other than that we wanna do all (or some) of them whenever we call our method. This is where the decorator pattern really helps.</p>
  <p>By implementing the decorator pattern we subscribe to the open-closed principal. Our method is open to future extension but closed to future modification. There's a lot of groovy benefits to obeying the open-closed principle.</p>
  </blockquote>
  <p>However, all the examples that I found are really complicated (e.g., writing HTTP servers with many middlewares). This make it difficult for me to apply the principle elsewhere. I need something that I can easily try on so as to wrap my head around it.</p>
  <p>Can someone give me an simpler example that can best illustrate how to do Decorator pattern (functions) in Go please?</p>
  <p><a href="https://gist.github.com/alehano/8785933" rel="noreferrer">This example by Alex Alehano</a>, is too simple to be put into practical use. I need something that can illustrate this:</p>
  <pre><code>func Decorate(c Decorated, ds ...Decorator) Decorated {
      decorated := c
      for _, decorate := range ds {
          decorated = decorate(decorated)
      }
      return decorated
  }
  </code></pre>
  <p>A string manipulation according to different option/instruction, e.g., to upper, to lower, to base64, etc, would be the best example IMO, and adding prefix/suffix too, as &quot;<a href="http://xion.io/post/code/go-decorated-functions.html" rel="noreferrer"><em>This technique proves especially valuable if the decorators themselves are parameterized</em></a>&quot;.</p>