Go-gin基础

Gin

安装
go get -u -v github.com/gin-gonic/gin

package main

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

type student struct {
	Name string
	Age  int8
}


var r = gin.Default()
func main() {
	runMain()
	r.Run(":8090")
}

func runMain() {
	MyGet()
	MyPost()
	GroupRoute()
	uploadFile()
	templateFun()
}

func templateFun() {
	r.LoadHTMLGlob("templates/*")
	stu1 := &student{Name: "Chuancy", Age: 20}
	stu2 := &student{Name: "Tty", Age: 22}
	r.GET("/arr", func(c *gin.Context) {
		c.HTML(http.StatusOK, "tom.tmpl", gin.H{
			"title":  "Gin",
			"stuArr": [2]*student{stu1, stu2},
		})
	})
}

func uploadFile() {
	fmt.Printf("tool.GetEnvDefault(\"name\", \"hhh\"): %v\n", tool.GetEnvDefault("name", "hhh"))
	//curl --location --request POST 'http://localhost:8090/upload' --form 'file=@"/D:/文档/calico/calico-recomand.yaml.yaml"'
	r.POST("/upload", func(ctx *gin.Context) {
		fh, err := ctx.FormFile("file")
		if err == nil {
			dest := "D:\\download\\" + fh.Filename
			ctx.SaveUploadedFile(fh, dest)
		} else {
			ctx.String(400, "%s upload failed!", fh.Filename)
		}
		ctx.String(200, "%s uploaded!!", fh.Filename)
	})

	r.POST("/uploads", func(ctx *gin.Context) {
		form, _ := ctx.MultipartForm()
		files := form.File["upload[]"]
		for _, file := range files {
			log.Print(file.Filename)
			dest := "D:\\download\\" + file.Filename
			ctx.SaveUploadedFile(file, dest)
		}
		ctx.String(200, "%d files uploaded!!", len(files))
	})
}

func MyGet() {
	// 无参get
	// curl http://localhost:8090/
	r.GET("/", func(ctx *gin.Context) {
		ctx.String(http.StatusOK, "无参GET")
	})

	r.GET("/index", func(ctx *gin.Context) {
		ctx.String(http.StatusOK, "index")
	})
	// 解析路径参数
	// curl http://localhost:8090/foo/chauncy
	r.GET("/foo/:name", func(ctx *gin.Context) {
		name := ctx.Param("name")
		ctx.String(200, "hello,%s", name)
	})

	// 获取Query参数
	// curl http://localhost:8090/users?name=chuancy&role=teacher
	r.GET("/users", func(ctx *gin.Context) {
		name := ctx.Query("name")
		role := ctx.DefaultQuery("role", "IT")
		ctx.String(200, "%s is a %s", name, role)
	})
}

func MyPost() {

	// curl http://localhost:8090/form  -X POST -d 'username=chauncy&password=1234'
	r.POST("/form", func(ctx *gin.Context) {
		username := ctx.PostForm("username")
		password := ctx.DefaultPostForm("password", "00000") // 默认值

		ctx.JSON(200, gin.H{
			"username": username,
			"password": password,
		})
	})

	// get post参数混合
	// curl --location --request POST 'localhost:8090/posts?id=0&page=12' --form 'username="chuancy"'
	r.POST("/posts", func(ctx *gin.Context) {
		id := ctx.Query("id")
		page := ctx.DefaultQuery("page", "0")
		username := ctx.PostForm("username")
		password := ctx.DefaultPostForm("password", "0000000")
		ctx.JSON(200, gin.H{
			"id":        id,
			"page":      page,
			"username":  username,
			"passowrod": password,
		})
	})

	// map参数
	// curl --location -g --request POST 'http://localhost:8090/post?ids[chauncy]=001&ids[jin]=002' --form 'names[foo]="tla"' --form 'names[bar]="Wiei"'
	r.POST("/post", func(ctx *gin.Context) {
		ids := ctx.QueryMap("ids")
		names := ctx.PostFormMap("names")
		ctx.JSON(200, gin.H{
			"ids":   ids,
			"names": names,
		})
	})

	// 重定向

	r.GET("/redirect", func(ctx *gin.Context) {
		ctx.Redirect(http.StatusMovedPermanently, "/index")
	})

	r.GET("/goindex", func(ctx *gin.Context) {
		ctx.Request.URL.Path = "/"
		r.HandleContext(ctx)
	})
}

// 分组路由
func GroupRoute() {
	defaultHandler := func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{
			"path": ctx.FullPath(),
		})
	}
	v1 := r.Group("/v1")
	v1.GET("/foo", defaultHandler)
	v1.GET("/bar", func(ctx *gin.Context) {
		ctx.String(200, "/v1/bar")
	})

	v2 := r.Group("/v2")
	{
		v2.GET("/posts", defaultHandler)
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值