前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

Gin 5

作者头像
franket
发布2021-08-10 15:01:41
4290
发布2021-08-10 15:01:41
举报
文章被收录于专栏:技术杂记技术杂记

前言

Gin 是一款用 Go(Golang) 编写的 web 框架

Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter

因为 httprouter, 它提供了更高的性能

这里演示一下 Gin 的 PostForm 传参

gin 的 API 可以参考 API REFFERENCE

Tip: 当前的版本为 Gin 1.2Go 1.10 (但是实验环境下,Go没有使用最新的版本)


操作

系统环境

代码语言:javascript
复制
[root@h160 ~]# hostnamectl 
   Static hostname: h160
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d46f9440d4be429ea66b726977adf233
           Boot ID: a0fc6a1b4f124e39a27866d4df0701fa
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.2.3.el7.x86_64
      Architecture: x86-64
[root@h160 ~]# ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:c9:c7:04 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 84363sec preferred_lft 84363sec
    inet6 fe80::5054:ff:fec9:c704/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:48:f4:2c brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.160/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe48:f42c/64 scope link 
       valid_lft forever preferred_lft forever
[root@h160 ~]# go version
go version go1.9.4 linux/amd64
[root@h160 ~]#

进入 GOPATH

代码语言:javascript
复制
[vagrant@h160 ~]$ echo $GOPATH
/vagrant/go
[vagrant@h160 ~]$ cd $GOPATH
[vagrant@h160 go]$ pwd
/vagrant/go
[vagrant@h160 go]$ 

代码注解

代码语言:javascript
复制
package main  //指明此包为 main 包

import "github.com/gin-gonic/gin" //导入 gin 包
import "fmt" // 导入 fmt 可以进行格式化输出

func main() {
	r := gin.Default()  // 使用 gin.Default() 方法生成一个引擎实例,这个实例默认情况下已经将 Logger Recovery 进行了装载
	r.POST("/form", func(c *gin.Context) {
		//DefaultPostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns the specified defaultValue string
		tag := c.DefaultPostForm("tag", "alert")//func (c *Context) DefaultPostForm(key, defaultValue string) string ,第二个参数为默认值
		//func (c *Context) PostForm(key string) string
		msg := c.PostForm("msg")
		title := c.PostForm("title")
		fmt.Printf("tag is %s, msg is %s, title is %s\n", tag, msg, title)
	})
	r.Run(":8080") //在 0.0.0.0:8080 上启监听
}

编译执行

代码语言:javascript
复制
[vagrant@h160 go]$ go run hello.go 
[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /form                     --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
tag is t1, msg is m1, title is test
[GIN] 2018/07/06 - 16:57:48 | 200 |      148.11µs |  192.168.56.105 | POST     /form
tag is alert, msg is m1, title is test
[GIN] 2018/07/06 - 16:57:59 | 200 |      66.528µs |  192.168.56.105 | POST     /form
tag is , msg is m1, title is test
[GIN] 2018/07/06 - 16:58:04 | 200 |      57.533µs |  192.168.56.105 | POST     /form
tag is , msg is , title is test
[GIN] 2018/07/06 - 16:58:12 | 200 |       77.04µs |  192.168.56.105 | POST     /form
tag is , msg is , title is 
[GIN] 2018/07/06 - 16:58:18 | 200 |      56.704µs |  192.168.56.105 | POST     /form
tag is , msg is , title is 
[GIN] 2018/07/06 - 16:58:23 | 200 |      64.421µs |  192.168.56.105 | POST     /form
tag is alert, msg is , title is 
[GIN] 2018/07/06 - 16:58:29 | 200 |      73.434µs |  192.168.56.105 | POST     /form
...
...
...

客户的请求为

代码语言:javascript
复制
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'tag=t1&msg=m1&title=test'
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'msg=m1&title=test'
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'tag=&msg=m1&title=test'
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'tag=&msg=&title=test'
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'tag=&msg=&title='
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d 'tag='
[vagrant@h105 ~]$ curl -XPOST 192.168.56.160:8080/form -d ''
[vagrant@h105 ~]$ 

参数直接输出到了 console


总结

构建一个简单的 web 还是非常快捷的

可以通过 context 的 PostForm 来获取参数

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 操作
    • 系统环境
      • 进入 GOPATH
        • 代码注解
          • 编译执行
          • 总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档