美文网首页
06. 项目结构

06. 项目结构

作者: 大鱼人Echo | 来源:发表于2019-05-30 23:09 被阅读0次
    ## 目录结构
    ├── api
    │   ├── dbops
    │   ├── defs
    │   ├── handlers.go
    │   └── main.go
    └── vendor
    
    package main
    
    import (
        "net/http"
    
        "github.com/julienschmidt/httprouter"
    )
    
    // RegisterHandlers 注册Handler
    func RegisterHandlers() *httprouter.Router {
        router := httprouter.New()
    
        router.POST("/user", CreateUser)
        router.POST("/user/:user_name", Login)
    
        return router
    }
    
    func main() {
        r := RegisterHandlers()
        http.ListenAndServe(":8000", r)
    }
    
    // 处理流程
    // listen -> RegisterHandlers -> handlers
    
    
    package main
    
    import (
        "io"
        "net/http"
    
        "github.com/julienschmidt/httprouter"
    )
    
    // CreateUser 创建用户
    func CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        io.WriteString(w, "Create User Handler")
    }
    
    // Login 用户登录
    func Login(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        uname := p.ByName("user_name")
        io.WriteString(w, "hello "+uname)
    }
    
    

    相关文章

      网友评论

          本文标题:06. 项目结构

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