gin框架提高篇(一)

重定向

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	// 外部链接
	r.GET("/redirect1", func(context *gin.Context) {
		url := "http://www.baidu.com"
		context.Redirect(http.StatusMovedPermanently, url)
	})
	
	// 自定义重定向
	r.GET("/redirect2", func(context *gin.Context) {
		context.Request.URL.Path = "/testRedirect"
		r.HandleContext(context)
	})
	
	// 自定义链接
	r.GET("/testRedirect", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "响应成功",
		})
	})
	
	r.Run(":9090")
}

在这里插入图片描述
在这里插入图片描述

获取第三方数据

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/getOtherData", func(context *gin.Context) {
		url := "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
		// 使用http.Get方法向指定的URL发送一个GET请求,并接收响应
		res, err := http.Get(url)
		// 检查是否在请求过程中发生错误,或者响应的状态码是否不是200(OK)
		if err != nil || res.StatusCode != http.StatusOK {
			context.Status(http.StatusServiceUnavailable)
			return
		}
		// 请求体内容
		body := res.Body
		// 请求体长度
		contentLen := res.ContentLength
		// 请求媒体类型
		contentType := res.Header.Get("Content-Type")
		// 将获取到的数据以及相关信息发送给客户端
		context.DataFromReader(http.StatusOK, contentLen, contentType, body, nil)
	})

	r.Run(":9090")
}

在这里插入图片描述

多形式渲染

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/json", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{
			"html": "<b>hello</b>",
		})
	})

	r.GET("/html", func(context *gin.Context) {
		context.PureJSON(http.StatusOK, gin.H{
			"html": "<b>hello</b>",
		})
	})

	r.GET("/xml", func(context *gin.Context) {
		type Message struct {
			Name string
			Msg  string
			Age  int
		}
		info := Message{}
		info.Name = "FAN"
		info.Msg = "hello"
		info.Age = 23
		context.XML(http.StatusOK, info)
	})

	r.GET("yaml", func(context *gin.Context) {
		context.YAML(http.StatusOK, gin.H{
			"message": "hello",
			"status":  200,
		})
	})
	r.Run(":9090")
}

在这里插入图片描述

文件服务器

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/file", fileServer)

	r.Run(":9090")
}

func fileServer(context *gin.Context) {
	// 存储路径
	path := "D:/2-golang/img/"
	// 文件名
	fileName := path + context.Query("name")
	context.File(fileName)
}

在这里插入图片描述

单文件上传

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.POST("/upload", func(context *gin.Context) {
		file, err := context.FormFile("fileName")
		if err != nil {
			context.String(http.StatusBadRequest, "文件上传错误!")
		}
		dst := "D:/2-golang/img/"
		context.SaveUploadedFile(file, dst+file.Filename)
		context.String(http.StatusOK, fmt.Sprintf("%s 上传成功", file.Filename))
	})

	r.Run(":9090")
}

在这里插入图片描述

在这里插入图片描述

多文件上传

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.POST("/allUpload", fileServe)
	r.Run(":9090")
}

func fileServe(context *gin.Context) {
	form, err := context.MultipartForm()
	if err != nil {
		context.String(http.StatusBadRequest, "上传文件错误!")
	}
	// 多文件上传原理是一样的,只是多了个循环来保存文件
	dst := "D:/2-golang/img/"
	files := form.File["file_key"]
	for _, file := range files {
		context.SaveUploadedFile(file, dst+file.Filename)
	}
	context.String(http.StatusOK, fmt.Sprintf("%d 个文件上传成功!", len(files)))
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值