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 main
import (
"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
post请求接收json
r.POST("/json", func(c *gin.Context) {
data,_ := c.GetRawData()
var m map[string]interface{}
_ = json.Unmarshal(data, &m)
c.JSON(http.StatusOK,m)
})
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。