06. 项目结构

## 目录结构
├── 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. 项目结构)