Core Concepts
Last updated
Was this helpful?
Was this helpful?
m.Get("/", myHandler)
m.Get("/hello", myHandler)
func myHandler() string {
return "hello world"
}m.Get("/", myHandler1, myHandler2)
func myHandler1() {
// ... do something
}
func myHandler2() string {
return "hello world"
}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>
})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"
})m.Get("/", func(resp http.ResponseWriter, req *http.Request) {
// resp and req are injected by Macaron
resp.WriteHeader(200) // HTTP 200
})m.Get("/", func(ctx *macaron.Context) {
ctx.Resp.WriteHeader(200) // HTTP 200
})m.Use(func() {
// do some middleware stuff
})m.Handlers(
Middleware1,
Middleware2,
Middleware3,
)// validate an api key
m.Use(func(ctx *macaron.Context) {
if ctx.Req.Header.Get("X-API-KEY") != "secret123" {
ctx.Resp.WriteHeader(http.StatusUnauthorized)
}
})