package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
//http://localhost:8080/hello?name=davie
engine.Handle("GET", "/hello", func(context *gin.Context) {
path := context.FullPath()
fmt.Println(path)
name := context.DefaultQuery("name", "hello")
fmt.Println(name)
//输出,返回给前端
context.Writer.Write([]byte("Hello ," + name))
})
//post
//http://localhost:8080//login
engine.Handle("POST", "/login", func(context *gin.Context) {
fmt.Println(context.FullPath())
userename := context.PostForm("username")
password := context.PostForm("password")
fmt.Println(userename)
fmt.Println(password)
context.Writer.Write([]byte(userename + "登录"))
})
engine.Run()
}
使用postman输入:
终端打印:
(2)deletepackage main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
//http://localhost:8080/hello?name=davie
engine.GET("/hello", func(context *gin.Context) {
fmt.Println(context.FullPath())
name := context.Query("name")
fmt.Println(name)
context.Writer.Write([]byte("Hello," + name))
})
engine.POST("/login,", func(context *gin.Context) {
fmt.Println(context.FullPath())
username, exist := context.GetPostForm("username")
if exist {
fmt.Println(username)
}
password, exist := context.GetPostForm("password")
if exist {
fmt.Println(password)
}
context.Writer.Write([]byte("Hello" + username))
})
engine.DELETE("/user/:id", func(context *gin.Context) {
userID := context.Param("id")
context.Writer.Write([]byte("delete 用户ID:" + userID))
})
engine.Run()
}
postman:
2.参数绑定package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
)
func main() {
engine := gin.Default()
// http://localhost:8080/hello?name=davie&classes=计算机
engine.GET("/hello", func(context *gin.Context) {
fmt.Println(context.FullPath())
var student Student
err := context.ShouldBindQuery(&student)
if err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(student.Name)
fmt.Println(student.Classes)
context.Writer.Write([]byte("hello," + student.Name))
})
engine.Run()
}
type Student struct {
Name string `form:"name"`
Classes string `form:"classes"`
}
网站输入:
http://localhost:8080/hello?name=curry&classes=计算机
得到:
终端得到:
网站输入:
http://localhost:8080/hello?name=davie&classes=软件工程
得到:
终端得到:
3.注册绑定
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
)
func main() {
engine := gin.Default()
engine.POST("/register", func(context *gin.Context) {
fmt.Println(context.FullPath())
var register Register
if err := context.ShouldBind(®ister); err != nil {
log.Fatal(err.Error())
return
}
fmt.Println(register.UserName)
fmt.Println(register.Phone)
context.Writer.Write([]byte(register.UserName + " Register"))
})
engine.Run()
}
type Register struct {
UserName string `form:"username"` //绑定
Phone string `form:"phone"`
Password string `form:"password"`
}
postman:
4.[]byte格式package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
engine.GET("/hellobyte", func(context *gin.Context) {
fullpath := "请求路径:" + context.FullPath()
fmt.Println(fullpath)
context.Writer.Write([]byte(fullpath))
})
engine.GET("/hellostring", func(context *gin.Context) {
fullpath := "请求路径:" + context.FullPath()
fmt.Println(fullpath)
context.Writer.WriteString(fullpath)
})
engine.Run()
}
5.JSon格式
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
engine.GET("/hellojson", func(context *gin.Context) {
fullpath := "请求路径:" + context.FullPath()
fmt.Println(fullpath)
context.JSON(200, map[string]interface{}{
"code": 1,
"message": "OK",
"data": fullpath,
})
})
engine.GET("/jsonstruct", func(context *gin.Context) {
fullpath := "请求路径:" + context.FullPath()
fmt.Println(fullpath)
resp := Response{Code: 1, Message: "ok", data: fullpath}
context.JSON(200, &resp)
})
engine.Run()
}
type Response struct {
Code int
Message string
Data interface{}
}
6.Group(路由组)



