Macaron Documentation
  • Welcome
  • Starter Guide
  • Core Concepts
  • Core Services
  • Custom Services
  • Middlewares
    • Routing
    • Templating
    • Gzip
    • Localization
    • Data Binding and Validation
    • Cache
    • Captcha
    • Session
    • Cross-Site Request Forgery
    • Embed Binary Data
    • Serving Multiple Sites
  • FAQs
  • 简体中文
    • 初学者指南
    • 核心概念
    • 核心服务
    • 自定义服务
    • 中间件和辅助模块
      • 路由模块
      • 模板引擎
      • Gzip 压缩
      • 应用本地化
      • 数据绑定与验证
      • 缓存管理(Cache)
      • 验证码服务
      • 会话管理(Session)
      • 跨域请求攻击(CSRF)
      • 嵌入二进制数据
      • 服务多个站点
    • 常见问题
Powered by GitBook
On this page
  • Minimal Example
  • Extended Example
  • Go Further

Was this helpful?

Starter Guide

Before we get started, one thing you should know is that this documentation does not teach you how to use Go. Instead, we will explore Macaron based on the basic Go knowledge you already have.

To install Macaron:

go get gopkg.in/macaron.v1

And upgrade Macaron in the future:

go get -u gopkg.in/macaron.v1

Minimal Example

Create a file called main.go, and type 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.

Method m.Get is for registering routes for HTTP GET method. In this case, we allow GET requests to root path / and has a Handler function to simply returns string Hello world! as response.

You may have questions about why the handler function can return a string as response? The magic is the Return Values, this is a special case/syntax for responding requests by string.

Finally, we call method m.Run to get server running. By default, Macaron Instances will listen on 0.0.0.0:4000.

Then, execute command go run main.go, you should see a log message is printed to the console:

[Macaron] listening on 0.0.0.0:4000 (development)

Now, open your browser and visit localhost:4000, victory!

Extended Example

Let’s modify the main.go and do some extended exercises.

package main

import (
    "log"
    "net/http"

    "gopkg.in/macaron.v1"
)

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
}

If you execute command go run main.go again, you’ll see string the request path is: / is on your screen.

So what’s different now?

First of all, we still use Classic Macaron and register route for HTTP GET method of root path /. We don’t use anonymous function anymore, but a named function called myHandler. Notice that there is no parentheses after function name when we register route because we do not call it at that point.

The function myHandler accepts one argument with type *macaron.Context and returns a string. You may notice that we didn’t tell Macaron what arguments should pass to myHandler when we register routes, and if you look at the m.Get method, you will see Macaron sees all handlers(macaron.Handler) as type interface{}. So how does Macaron know?

This is related to the concept of Service Injection, *macaron.Context is one of the default injected services, so you can use it directly. Don’t worry about how to inject your own services, it’s just not the time to tell you yet.

Like the previous example, we need to make server listen on a address. This time, we use function from Go standard library called http.ListenAndServe, which shows any Macaron Instance is fully compatible with Go standard library.

Go Further

You now know about how to write simple code based on Macaron, please try to modify two examples above and make sure you fully understand the words you read.

When you feel comfortable, get up and keep reading on following chapters.

PreviousWelcomeNextCore Concepts

Last updated 5 years ago

Was this helpful?