Gin框架
web开发本质
go mod init 项目名
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func sayHello(w http.ResponseWriter,r *http.Request){
b,_:=ioutil.ReadFile("./hello.txt")
_,_=fmt.Fprintln(w,string(b))
}
func main() {
http.HandleFunc("/hello",sayHello)
err:=http.ListenAndServe(":8080",nil)
if err!=nil{
fmt.Println("http serve failed...")
return
}
}
gin框架初识
下载
go get -u github.com/gin-gonic/gin
package main
import(
"github.com/gin-gonic/gin"
)
func sayHello(c *gin.Context){
c.JSON(200,gin.H{
"message":"hello golang",
})
}
func main() {
r:=gin.Default()
r.GET("hello",sayHello)
//启动服务
r.Run()
}
gin框架返回json
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
// gin.H 是map[string]interface{}的缩写
r.GET("/someJSON", func(c *gin.Context) {
// 方式一:自己拼接JSON
c.JSON(http.StatusOK, gin.H{"message": "Hello world!"})
})
r.GET("/moreJSON", func(c *gin.Context) {
// 方法二:使用结构体
var msg struct {
Name string `json:"user"`
Message string
Age int
}
msg.Name = "小王子"
msg.Message = "Hello world!"
msg.Age = 18
c.JSON(http.StatusOK, msg)
})
r.Run(":8080")
}
gin返回query参数
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/web", func(ctx *gin.Context) {
name := ctx.Query("query")
age := ctx.Query("age")
ctx.JSON(http.StatusOK, gin.H{
"name": name,
"age": age,
})
})
r.Run()
}
获取form表单参数
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.LoadHTMLFiles("./login.html")
r.GET("/login",func(ctx *gin.Context) {
ctx.HTML(http.StatusOK,"login.html",nil)
})
r.POST("/login", func(c *gin.Context) {
// DefaultPostForm取不到值时会返回指定的默认值
//username := c.DefaultPostForm("username", "小王子")
username := c.PostForm("username")
address := c.PostForm("address")
//输出json结果给调用方
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"username": username,
"address": address,
})
})
err := r.Run()
if err != nil {
return
}
}
获取url路径参数
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/user/:username/:age", func(c *gin.Context) {
username := c.Param("username")
age := c.Param("age")
//输出json结果给调用方
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"username": username,
"address": age,
})
})
err := r.Run()
if err != nil {
return
}
}
gin框架使用参数绑定(json)
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
type Login struct {
User string `form:"user" json:"user" binding:"required"`
Password string `form:"password" json:"password" binding:"required"`
}
r.POST("/json", func(c *gin.Context) {
var login Login
if err := c.ShouldBind(&login); err == nil {
fmt.Printf("login info:%#v\n", login)
c.JSON(http.StatusOK, gin.H{
"user": login.User,
"password": login.Password,
})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
})
err := r.Run()
if err != nil {
return
}
}
![image-20230221105357180](https://imguploadweiqi.oss-cn-guangzhou.aliyuncs.com/image-20230221105357180.png)
gin框架重定向
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
//http重定向
r.GET("/test", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "https://www.baidu.com/")
})
r.GET("/test2", func(c *gin.Context) {
// 指定重定向的URL
c.Request.URL.Path = "/test3"
r.HandleContext(c)
})
r.GET("/test3", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"hello": "world"})
})
err := r.Run()
if err != nil {
return
}
}
gin框架路由组
func main() {
r := gin.Default()
userGroup := r.Group("/user")
{
userGroup.GET("/index", func(c *gin.Context) {...})
userGroup.GET("/login", func(c *gin.Context) {...})
userGroup.POST("/login", func(c *gin.Context) {...})
}
shopGroup := r.Group("/shop")
{
shopGroup.GET("/index", func(c *gin.Context) {...})
shopGroup.GET("/cart", func(c *gin.Context) {...})
shopGroup.POST("/checkout", func(c *gin.Context) {...})
}
r.Run()
}
标签:http,Context,框架,GET,学习,func,gin,main
From: https://www.cnblogs.com/hwq1009/p/17140762.html