install gin

  • 在终端中执行 呦

    % go env -w GOPROXY=https://goproxy.cn,direct$ go get -u github.com/gin-gonic/gin
  • 在 golang的idea 中搞
% go mod init awesomeProject

得呈现 go.mod 才行

code

package mainimport (    "fmt"    "github.com/gin-gonic/gin"    "log"    "net/http"    "time")func m1(c *gin.Context)  {    fmt.Println("m1 in..")    start := time.Now()    c.Abort()    cost := time.Since(start)    fmt.Printf("cost: %v", cost)}func index(c *gin.Context)  {    c.JSON(http.StatusOK, gin.H{        "msg": "ok",    })}type Userinfo struct {    Username string `form:"username"`    Password string `form:"password"`}func main() {    r := gin.Default()    r.LoadHTMLGlob("./templates/*")    //定义路由的GET办法及响应处理函数    r.GET("/hello", func(c *gin.Context) {        //将发送的信息封装成JSON发送给浏览器        c.JSON(http.StatusOK, gin.H{            //这是咱们定义的数据            "message": "疾速入门",        })    })    r.NoRoute(func(c *gin.Context) {        c.HTML(http.StatusNotFound,"templates/404.html",nil)    })    r.GET("/student", func(c *gin.Context) {        c.JSON(http.StatusOK, gin.H{            "message": "cha xun student mes success",        })    })    r.POST("/create_student", func(c *gin.Context) {        c.JSON(http.StatusOK, gin.H{            "message": "create student success",        })    })    r.PUT("/updata_student", func(c *gin.Context) {        c.JSON(http.StatusOK, gin.H{            "message": "更新学生信息胜利",        })    })    r.DELETE("/delete_student", func(c *gin.Context) {        c.JSON(http.StatusOK, gin.H{            "message": "删除学生信息胜利",        })    })    r.GET("/demo", func(c *gin.Context) {        c.HTML(http.StatusOK, "index.html", gin.H{            "name": "admin",            "pwd": "123456",        })    })    r.GET("/user/:username", func(c *gin.Context) {        username := c.Param("username")        c.JSON(http.StatusOK, gin.H{            "username": username,        })    })        user := r.Group("/user")    user.GET("/index", func(c *gin.Context) {            })    user.POST("/login", func(c *gin.Context) {            })    //r.GET("/", func(c *gin.Context) {    //    name := c.Query("name")    //    pwd := c.Query("pwd")    //    c.JSON(http.StatusOK, gin.H{    //        "name": name,    //        "pwd": pwd,    //    })    //})    r.GET("/", m1, index)    r.GET("/long_async", func(c *gin.Context) {        // 创立在 goroutine 中应用的正本        tmp := c.Copy()        go func() {            // 用 time.Sleep() 模仿一个长工作。            time.Sleep(5 * time.Second)            // 请留神您应用的是复制的上下文 "tmp",这一点很重要            log.Println("Done! in path " + tmp.Request.URL.Path)        }()    })    r.GET("/long_sync", func(c *gin.Context) {        // 用 time.Sleep() 模仿一个长工作。        time.Sleep(5 * time.Second)        // 因为没有应用 goroutine,不须要拷贝上下文        log.Println("Done! in path " + c.Request.URL.Path)    })    r.GET("/user", func(c *gin.Context) {        var u Userinfo        err := c.ShouldBind(&u)        if err != nil {            c.JSON(http.StatusBadGateway, gin.H{                "error": err.Error(),            })        }else {            c.JSON(http.StatusOK, gin.H{                "status": "ok",            })        }        fmt.Printf("%#v\n", u)    })    r.GET("/upload", func(c *gin.Context) {        c.HTML(http.StatusOK, "upload.html", gin.H{            "mess": "mess",        })    })    r.POST("/upload", func(c *gin.Context) {        file, err := c.FormFile("f1")        if err != nil {            c.JSON(http.StatusInternalServerError, gin.H{                "message": err.Error(),            })            return        }        log.Println(file.Filename)        dst := fmt.Sprintf("./dst/%s", file.Filename)        c.SaveUploadedFile(file, dst)        c.JSON(http.StatusOK, gin.H{            "message": fmt.Sprintf("'%s' uploaded!", file.Filename),        })    })    r.Run() //默认在本地8080端口启动服务}

test

  • 申明并创立前端模版文件夹和upload目录

    拜访 本机8080+url