美文网首页
gofream框架01 入门

gofream框架01 入门

作者: 王哈哈就很棒 | 来源:发表于2019-12-19 19:51 被阅读0次

    使用gfcli创建项目

    https://goframe.org/toolchain/cli

    USAGE
        gf COMMAND [ARGUMENT] [OPTION]
    
    COMMAND
        get        install or update GF to system in default...
        gen        automatically generate go files for ORM models...
        init       initialize an empty GF project at current working directory...
        help       show more information about a specified command
        pack       packing any file/directory to a resource file, or a go file
        build      cross-building go project for lots of platforms...
        update     update current gf binary to latest one (you may need root/admin permission)
        install    install gf binary to system (you may need root/admin permission)
        version    show version info
    
    OPTION
        -?,-h      show this help or detail for specified command
        -v,-i      show version information
    
    ADDITIONAL
        Use 'gf help COMMAND' or 'gf COMMAND -h' for detail about a command, which has '...' in the tail of their comments.
    

    不使用gocli直接写

    package main
    
    import (
        "github.com/gogf/gf/frame/g"
        "github.com/gogf/gf/net/ghttp"
    )
    
    func main() {
        s := g.Server()
    
        // 使用原生的模板引擎gview模块来实现模板管理
        v := g.View()
        // 设置解析变量分隔符
        v.SetDelimiters("[[", "]]")
        // 添加当前模板引擎对象的模板目录路径
        v.AddPath("./templates")
    
        // 返回html文本
        s.BindHandler("/", func(r *ghttp.Request) {
            r.Response.Write("<h1>hello</h1>")
        })
    
        // 返回JSON
        s.BindHandler("/json", func(r *ghttp.Request) {
            var data = struct{
                Name string `json:"name"`
                Age int `json:"age"`
            }{
                "admin",
                23,
            }
            r.Response.WriteJson(data)
        })
    
        // 返回XML
        s.BindHandler("/xml", func(r *ghttp.Request) {
            var data = struct{
                Name string `json:"name"`
                Age int `json:"age"`
            }{
                "admin",
                23,
            }
            r.Response.WriteXml(data)
        })
    
        // 返回HTML
        s.BindHandler("/html", func(r *ghttp.Request) {
            r.Response.WriteTpl("index.html")
        })
    
        s.Run()
    }
    

    相关文章

      网友评论

          本文标题:gofream框架01 入门

          本文链接:https://www.haomeiwen.com/subject/dusvnctx.html