Gin返回json
1.使用map[string]interfave{} 或者gin内置的gin.H{}
2.使用struct 也可以进行使用tag来对struct字段做定制化操作
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/json", func(c *gin.Context) {
//方法1:使用map序列化
data := map[string]interface{}{
"name": "小王子",
"message": "饿了",
"age": 10,
}
c.JSON(http.StatusOK, data) //将json数据返回给请求方
})
//方法2:结构体
type msg struct {
Name string `json:"我的"`
Message string
Age int
}
r.GET("/another_json", func(c *gin.Context) {
data := msg{
Name: "张三",
Message: "Hello world",
Age: 10,
}
c.JSON(http.StatusOK, data)
})
r.Run(":9090")
}
Gin获取querystring参数
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
//GET请求 ?后面跟的是querystring参数
//key=value格式,多个key-value用 & 连接
//eg: /web?query=我&age=10
r.GET("web", func(c *gin.Context) {
//获取浏览器那边发请求携带的query string 参数
//1. name := c.Query("query") //通过query获取请求携带中的query string参数
//2. name := c.DefaultQuery("query", "someone") //没有参数就会返回默认值
//3. name, ok := c.GetQuery("query")
name := c.Query("query")
age := c.Query("age")
//if !ok { //取不到
// name = "some one"
//
//}
c.JSON(http.StatusOK, gin.H{ //前端页面显示满足字典序
"name": name,
"age": age,
})
})
r.Run(":9090")
}
Gin from表单
与query一样 可以使用 c.postfrom() c.defaultpostfrom() c.GetPostForm() string bool 除了main文件还有两个html文件 c 是 *gin.Context类型
package main
//获取form表单提交的参数
/*
点击登录后往哪里发是由from的action属性决定的
*/
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.LoadHTMLFiles("./login.html", "./index.html")
r.GET("login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.html", "ok")
})
// /login post
r.POST("/login", func(c *gin.Context) {
//获取from表单提交的数据
//username := c.PostForm("username")
//password := c.PostForm("password")
username := c.DefaultPostForm("username", "somebody")
password := c.DefaultPostForm("xxx", "***")
c.GetPostForm()
c.HTML(http.StatusOK, "index.html", gin.H{
"Name": username,
"Password": password,
})
})
r.Run(":9090")
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>Hello,{{.Name}}!</h1>
<p>你的密码是:{{.Password}}!</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="/login" method="post" novalidate autocomplete="off">
<label for="username">username:</label>
<input type="text" name="username" id="username">
<label for="password">password:</label>
<input type="password" name="password" id="password">
<input type="submit" value="登录">
</form>
</body>
</html>
Gin 获取URL路径参数 + Gin参数绑定-ShouldBind
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
//获取请求的path(URL)参数 ,返回的都是字符串类型
func main() {
r := gin.Default()
r.GET("/:name/:age", func(c *gin.Context) {
//获取路径参数
name := c.Param("name")
age := c.Param("age")
c.JSON(http.StatusOK, gin.H{
"name": name,
"age": age,
})
})
r.GET("/blog/:year/:month", func(c *gin.Context) {
year := c.Param("year")
month := c.Param("month")
c.JSON(http.StatusOK, gin.H{
"year": year,
"month": month,
})
})
r.Run(":9090")
}
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type UserInfo struct {
Username string `form:"username"`
Password string `form:"password"`
}
func main() {
r := gin.Default()
r.LoadHTMLFiles("./index.html")
r.GET("/user", func(c *gin.Context) {
var u UserInfo //声明一个UserInfo类型的变量
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"name": u.Username,
"pass": u.Password,
})
}
})
r.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil) //渲染一个页面
})
r.POST("/form", func(c *gin.Context) {
var u UserInfo //声明一个UserInfo类型的变量
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"name": u.Username,
"pass": u.Password,
})
}
})
r.Run(":9090")
}
ShouldBind用于获取请求中携带的参数
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="/form" method="post">
用户名:
<input type="text" name="username">
密码:
<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>