Gin 是一个 go 写的 web 框架,具有高性能的优点。官方地址:https://github.com/gin-gonic/gin
背景
ide: GoLand
安装
go get -u github.com/gin-gonic/gin
使用
新建一个testGin.go文件
通过引用github.com/gin-gonic/gin
,在8080
端口启动
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
下面是一些实践案例
api参数
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 创建路由
r := gin.Default()
// 路由访问规则
r.GET("/user/:name/*action", func(context *gin.Context) {
name := context.Param("name") // 获取name
action := context.Param("action") // 获取action
context.String(http.StatusOK, name + "is" + action)
})
r.Run()
}
Url参数
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/sayhello", func(context *gin.Context) {
name := context.DefaultQuery("name", "Larmy") //默认给name赋值一下
context.String(http.StatusOK, fmt.Sprintf("htllo %s", name))
})
r.Run()
}
当没有携带name参数时,默认显示Larmy
携带name参数
表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8080/form" method="post" enctype="application/x-www-form-urlencoded">
User:<input type="text" name="username">
<br>
Pass:<input type="password" name="password" >
<br>
love:
<input type="radio" value="eat"name="hobby">吃饭
<input type="radio" value="sleep"name="hobby">睡觉
<input type="radio" value="hit douodu"name="hobby">打豆豆
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/form", func(context *gin.Context) {
type1 := context.DefaultQuery("type", "alert")
username := context.PostForm("username")
password := context.PostForm("password")
hobbies := context.PostForm("hobby")
context.String(http.StatusOK, fmt.Sprintf("type is %s,username is %s,password is %s,hobby is %v", type1, username, password, hobbies))
})
r.Run()
}
上传单个文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://127.0.0.1:8080/upload" method="post" enctype="multipart/form-data">
头像:
<input type="file" name="file">
<br>
<input type="submit" value="commit">
</form>
</body>
</html>
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/upload", func(context *gin.Context) {
file, err := context.FormFile("file")
if err != nil {
log.Println("出错err:", err)
}
log.Println("fileName:", file.Filename) // 打印日志到控制台
context.SaveUploadedFile(file, file.Filename) // 把照片保存到根目录
context.String(http.StatusOK, fmt.Sprintf("%s upload!", file.Filename))
})
r.Run()
}
可以在项目目录看到上传的图片,已经在控制台看到打印的log信息
上传多个文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://127.0.0.1:8080/upload" method="post" enctype="multipart/form-data">
头像:
<input type="file" name="files" multiple>
<br>
<input type="submit" value="commit">
</form>
</body>
</html>
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.POST("/upload", func(context *gin.Context) {
form, err := context.MultipartForm()
if err != nil {
context.String(http.StatusBadRequest, fmt.Sprintf("err %s"), err.Error())
} else {
files := form.File["files"]
for _, file := range files {
if err := context.SaveUploadedFile(file, file.Filename); err != nil {
context.String(http.StatusBadRequest, fmt.Sprintf("upload err: %s", err.Error()))
return
}
}
context.String(http.StatusOK, fmt.Sprintf("upload ok %d files", len(files)))
}
})
r.Run()
}
路由组
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
v1 := r.Group("v1")
{
v1.GET("/login", login)
v1.GET("/sayHi", sayHi)
}
v2 := r.Group("v2")
{
v2.GET("/login", login)
v2.GET("/sayHi", sayHi)
}
r.Run()
}
func login(ctx *gin.Context) {
name := ctx.DefaultQuery("name", "ljm")
ctx.String(http.StatusOK, fmt.Sprintf("%s\n login", name))
}
func sayHi(ctx *gin.Context) {
name := ctx.DefaultQuery("name", "LJM")
ctx.String(http.StatusOK, fmt.Sprintf("hello %s\n", name))
}