days
1
0
hours
0
8
minutes
2
0
seconds
3
3
Guess again

Macaron — The “sweetest” web framework in Go

Gabriela Motroc
#go
Macaron

© Shutterstock / AppleZoomZoom

A wolf in sheep’s clothing. Macaron may sound like a delicious French dessert but this idea couldn’t be further from the truth. Macaron is a highly productive and modular web framework in Go which takes basic ideology from Martini and extends in advance. Plus, the logo is a dragon called Maca (there’s an explanation for that!).

Package macaron is a highly productive and modular web framework in Go.  It takes basic ideology from Martini and extends in advance. The minimum requirement of Go is 1.3.

Caution: This documentation does not teach you how to use Go. Instead, it explores Macaron based on the basic Go knowledge you already have.

Features

  • Powerful routing with suburl.
  • Flexible routes combinations.
  • Unlimited nested group routers.
  • Directly integrate with existing services.
  • Dynamically change template files at runtime.
  • Allow to use in-memory template and static files.
  • Easy to plugin/unplugin features with modular design.
  • Handy dependency injection powered by inject.
  • Better router layer and less reflection make faster speed.

To install Macaron

go get gopkg.in/macaron.v1

Upgrade Macaron in the future

go get -u gopkg.in/macaron.v1

Basic usage of Macaron

First, create a file called main.go and type the following code:

 
package main

import "gopkg.in/macaron.v1"

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

Function macaron.Classic creates and returns a Classic Macaron, the documentation shows.

Method m.Get is used for registering routes for HTTP GET method. In this case, GET requests are allowed to root path / while a Handler function is used to simply return string Hello world! as response.

If you have questions about why the handler function can return a string as response, the answer is the Return Values, a special case/syntax for responding requests by string.

Call method m.Run is used to get the server running. By default, Macaron Instances will listen on 0.0.0.0:4000.

You need to execute command go run main.go and then you should see a log message printed to the console:

[Macaron] listening on 0.0.0.0:4000 (development)

Last but not least, open your browser and visit localhost:4000.

If you want to see an extended example, check out the framework’s documentation.

The heart and soul of Macaron

Handlers are the heart and soul of Macaron. A handler is basically any kind of callable function:

m.Get("/", func() string {
    return "hello world"
})

The non-anonymous function is also allowed for the purpose of using it in multiple routes:

m.Get("/", myHandler)
m.Get("/hello", myHandler)

func myHandler() string {
    return "hello world"
}

It’s worth knowing that one route can have as many as handlers as you want to register with:

m.Get("/", myHandler1, myHandler2)

func myHandler1() {
    // ... do something
}

func myHandler2() string {
    return "hello world"
}

Return values

If a handler returns something, Macaron will write the result to the current http.ResponseWriter as a string:

m.Get("/", func() string {
    return "hello world" // HTTP 200 : "hello world"
})

m.Get("/", func() *string {
    str := "hello world"
    return &str // HTTP 200 : "hello world"
})

m.Get("/", func() []byte {
    return []byte("hello world") // HTTP 200 : "hello world"
})

m.Get("/", func() error {
    // Nothing happens if returns nil
    return nil 
}, func() error {
    // ... get some error
    return err // HTTP 500 : <error message>
})

You can also optionally return a status code (only applies to string and []byte types):

m.Get("/", func() (int, string) {
    return 418, "i'm a teapot" // HTTP 418 : "i'm a teapot"
})

m.Get("/", func() (int, *string) {
    str := "i'm a teapot"
    return 418, &str // HTTP 418 : "i'm a teapot"
})

m.Get("/", func() (int, []byte) {
    return 418, []byte("i'm a teapot") // HTTP 418 : "i'm a teapot"
})

Check out the entire list of core concepts here.

FAQs

What’s the idea behind this other than Martini?

  • Integrate frequently used middlewares and helper methods with less reflection.
  • Replace default router with faster multi-tree router.
  • To make much easier power Gogs project.
  • Make a deep source study against Martini.

Why is the logo a dragon? Shouldn’t it be some sort of dessert?

The transliteration of Macaron in Chinese is Maca Long, Long means dragon, so actually, the logo is a dragon whose name is Maca.

Have a look at the other questions answered here

Author
Gabriela Motroc
Gabriela Motroc is editor of JAXenter.com and JAX Magazine. Before working at S&S Media she studied International Communication Management at The Hague University of Applied Sciences.

Comments
comments powered by Disqus