学习web框架gin
- demo代码
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"log"
"os"
"io"
)
/**
* gin demo代码
*/
func main() {
/**
* 创建默认中间件
*/
router := gin.Default()
// Disable Console Color
gin.DisableConsoleColor()
/**
*简单测试
*/
router.GET("/someGet", getting)
router.POST("/somePost", posting)
router.PUT("/somePut", putting)
router.DELETE("/someDelete", deleting)
router.PATCH("/somePatch", patching)
router.HEAD("/someHead", head)
router.OPTIONS("/someOptions", options)
//How to write log file
//gin.DisableConsoleColor()
f, _ := os.Open("tmp/ginlog.log")
//两个地方存放日志记录
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
/**
*json返回
*/
router.GET("/ping", func(context *gin.Context) {
context.JSON(200, gin.H{
"message": "pong",
})
})
router.GET("/user/:name", func(context *gin.Context) {
name := context.Param("name")
context.String(http.StatusOK, "Hello, %s!", name)
})
router.GET("/user/:name/*action", func(context *gin.Context) {
name := context.Param("name")
action := context.Param("action")
message := name + " is " + action
context.String(http.StatusOK, message)
})
/**
* Querystring parameters
*/
router.GET("/welcome", func(context *gin.Context) {
firstName := context.DefaultQuery("firstName", "Guest")
lastName := context.Query("lastName")
context.String(http.StatusOK, "Hello, %s-%s", firstName, lastName)
})
/**
* Multipart/Urlencoded Form
*/
router.POST("/postForm", func(context *gin.Context) {
message := context.PostForm("message")
nick := context.DefaultPostForm("nick", "anonymous")
fmt.Println(message, nick)
context.JSON(http.StatusOK, gin.H{
"status": "posted",
"message": message,
"nick": nick,
})
})
/**
*处理post表单参数
*/
router.POST("/post", func(context *gin.Context) {
id := context.Query("id")
page := context.DefaultQuery("page", "0")
name := context.PostForm("name")
message := context.PostForm("message")
context.String(http.StatusOK, "id=%s, page=%s, name=%s, message=%s\n", id, page, name, message)
})
/**
*单文件处理
*/
router.POST("/upload", func(context *gin.Context) {
file, err := context.FormFile("file")
if err == nil {
log.Println(file.Filename)
err = context.SaveUploadedFile(file, "tmp/" + file.Filename)
if err == nil {
context.String(http.StatusOK, "%s success uploaded!", file.Filename)
}
} else {
fmt.Println("err:", err)
}
})
/**
*多文件处理
*/
router.POST("/uploadfiles", func(context *gin.Context) {
form, err := context.MultipartForm()
if err == nil {
files := form.File["files"]
for _, file := range files {
log.Println(file.Filename)
err = context.SaveUploadedFile(file, "tmp/" + file.Filename)
if err == nil {
context.String(http.StatusOK, "%s success uploaded!\n", file.Filename)
}
}
} else {
fmt.Println("err:", err)
}
})
/**
*router.Run(":3000")
*默认8080端口
*/
router.Run()
}
func getting(context *gin.Context) {
fmt.Println("test getting")
}
func posting(context *gin.Context) {
fmt.Println("test posting")
}
func putting(context *gin.Context) {
fmt.Println("test putting")
}
func deleting(context *gin.Context) {
fmt.Println("test deleting")
}
func patching(context *gin.Context) {
fmt.Println("test patching")
}
func head(context *gin.Context) {
fmt.Println("test head")
}
func options(context *gin.Context) {
fmt.Println("test options")
}
- 接口测试工具:postman
打卡时间: 23:25
有疑问加站长微信联系(非本文作者)