6 QuickStart
Lunny Xiao edited this page 2019-11-27 09:08:08 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

快速入门

安装Tango

go get gitea.com/lunny/tango

一个经典的Tango例子如下

package main

import (
    "errors"
    "gitea.com/lunny/tango"
)

type Action struct {
    tango.JSON
}

func (Action) Get() interface{} {
    if true {
        return map[string]string{
            "say": "Hello tango!",
        }
    }
    return errors.New("something error")
}

func main() {
    t := tango.Classic()
    t.Get("/", new(Action))
    t.Run()
}

然后在浏览器访问http://localhost:8000, 将会得到一个json返回

{"say":"Hello tango!"}

如果将上述例子中的 true 改为 false, 将会得到一个json返回

{"err":"something error"}

这段代码因为拥有一个内嵌的tango.JSON所以返回值会被自动的转成Json

HTML输出请查看开发文档中的Renders中间件