GopherAcademy
Jiahua Chen
Dec 6, 2014 2 min read

Macaron: Martini-style, but faster and cheaper

Macaron is a high productive and modular design web framework in Go. It takes basic ideology of Martini and extends in advance.

Why another web framework?

The story began with the Gogs project, it first uses Martini as its web framework, worked quite well. Soon after, our team found that Martini is good but too minimal, also too many reflections that cause performance issue. Finally, I came up an idea that why don’t we just integrate most frequently used middlewares as interfaces(huge reduction for reflection), and replace default router layer with faster one. It turns out Macaron actually requires less memory overhead for every request and faster speed than Martini, and at the same time, it reserves my favorite Martini-style coding.

Examples

If you are using Martini already, you shouldn’t be surprised with following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package main

import "github.com/Unknwon/macaron"

func main() {
    m := macaron.Classic()
    m.Get("/", func() string {
        return "Hello world!"
    })
    m.Run()
}

And here is another simple example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
    "log"
    "net/http"

    "github.com/Unknwon/macaron"
)

func main() {
    m := macaron.Classic()
    m.Get("/", myHandler)

    log.Println("Server is running...")
    log.Println(http.ListenAndServe("0.0.0.0:4000", m))
}

func myHandler(ctx *macaron.Context) string {
    return "the request path is: " + ctx.Req.RequestURI
}

How about middlewares?

There are already many middlewares to simplify your work:

  • binding - Request data binding and validation
  • i18n - Internationalization and Localization
  • cache - Cache manager
  • session - Session manager
  • csrf - Generates and validates csrf tokens
  • captcha - Captcha service
  • pongo2 - Pongo2 template engine support
  • sockets - WebSockets channels binding
  • bindata - Embed binary data as static and template files
  • toolbox - Health check, pprof, profile and statistic services
  • oauth2 - OAuth 2.0 backend
  • switcher - Multiple-site support
  • method - HTTP method override
  • permissions2 - Cookies, users and permissions
  • renders - Beego-like render engine(Macaron has built-in template engine, this is another option)

Want to know more about Macaron? Check out its documentation site!