go实践三 web服务器搭建 加载第三方包gin

Gin是一个golang的web微框架,封装比较优雅,API友好,源码注释比较明确,已经发布了1.0版本。具有快速灵活,容错方便等特点。该官网的说法是它具有更好的性能,有多好呢?据说是速度提升了40倍,所以这也是我们今后web开发中主要使用的一个框架。

gin的github地址:https://github.com/gin-gonic/gin

安装gin

一、可以访问国外网站,直接运行命令

go get -u github.com/gin-gonic/gin

二、不能访问国外网站,同时go的版本是1.11或以上,使用下面步骤:

linux行命令输入

# 从 Go 1.11 版本开始,还新增了 GOPROXY 环境变量,如果设置了该变量,下载源代码时将会通过这个环境变量设置的代理地址,而不再是以前的直接从代码库下载
[root@localhost go]# vi /etc/profile

最后一行的下一行添加
export GO111MODULE=on
export GOPROXY=https://goproxy.io

:wq 保存退出

[root@localhost go]# source /etc/profile


# 此时设置了代理,可以直接 使用 go get命令安装第三方包

[root@localhost go]# go get -u github.com/gin-gonic/gin

#*******************go 模块**********************
# 从 Go 1.11 开始,Go 允许在 $GOPATH/src 外的任何目录下使用 go.mod 创建项目。在 $GOPATH/src 中,为了兼容性,Go 命令仍然在旧的 GOPATH 模式下运行
#创建web目录 ,并切换到web
[root@localhost go]# mkdir web
[root@localhost go]# cd web

# 使用命令 go mod init web ,初始完成一个web module ,自动生成一个 go.mod文件
[root@localhost web]# go mod init web
[root@localhost web]# ll
-rw-------. 1 root root   121 Jul  6 16:30 go.mod

#查看 go.mod内容
[root@localhost web]# cat go.mod
module web

go 1.12

 

在web目录下,编辑一个 testgin.go 文件,内容如下

使用 go run testgin.go 运行该文件即可

package main

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

func Login(c *gin.Context)  {
	fmt.Fprintf(c.Writer,"Welcome index!")
}

func main() {
	router := gin.Default()
	router.GET("/",Login)

	router.GET("/user/:name", func(c *gin.Context) {
		name := c.Param("name")
		c.String(http.StatusOK, "Hello %s", name)
	})

	router.GET("/user/:name/*action", func(c *gin.Context) {
		name := c.Param("name")
		action := c.Param("action")
		message := name + " is " + action
		c.String(http.StatusOK, message)
	})

	router.POST("/your/:name/*action", func(c *gin.Context) {
		name := c.Param("name")
		action := c.Param("action")
		message := name + " is " + action
		c.String(http.StatusOK, message)
	})

	router.Run("10.10.87.243:8080")
}

运行完go run testgin.go 文件后,我们再来看一下 go.mod文件的内容

[root@localhost web]# cat go.mod
module web

go 1.12

require (
	github.com/gin-gonic/gin v1.4.0 // indirect
)

## go.mod文件内容发生变化, 他会记录你曾经加载过的第三方包

## 同时web目录下也多了一个 go.sum 文件,这是我们直接引用的package和它自身需要的以来的版本记录,go modules就是根据这些去找到需要的packages的。

[root@localhost web]# ll
-rw-------. 1 root root    97 Jul  6 16:40 go.mod
-rw-------. 1 root root 14632 Jul  6 16:30 go.sum

 

运行成功,将会监听服务器对应端口

[GIN-debug] GET    /                         --> main.Login (3 handlers)
[GIN-debug] GET    /user/:name               --> main.main.func1 (3 handlers)
[GIN-debug] GET    /user/:name/*action       --> main.main.func2 (3 handlers)
[GIN-debug] POST   /your/:name/*action       --> main.main.func3 (3 handlers)
[GIN-debug] Listening and serving HTTP on 10.10.87.243:8080

访问方式:使用浏览器访问地址ip:端口号  http://10.10.87.243:8080/ 

Welcome index!

http://10.10.87.243:8080/user/world

Hello world

 

服务器端可以监听到访问信息:

[GIN-debug] GET    /                         --> main.Login (3 handlers)
[GIN-debug] GET    /user/:name               --> main.main.func1 (3 handlers)
[GIN-debug] GET    /user/:name/*action       --> main.main.func2 (3 handlers)
[GIN-debug] POST   /your/:name/*action       --> main.main.func3 (3 handlers)
[GIN-debug] Listening and serving HTTP on 10.10.87.243:8080
[GIN] 2019/07/06 - 14:56:01 | 200 |        9.98µs |    10.10.87.220 | GET      /
[GIN] 2019/07/06 - 14:56:48 | 200 |       30.57µs |    10.10.87.220 | GET      /user/world

 

参考链接:

go代理安装第三方包:https://github.com/overnote/golang/blob/master/01-Go%E5%88%9D%E8%AF%86/03-%E5%8C%85%E4%B8%8E%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86.md

go使用gin包:https://www.cnblogs.com/Survivalist/articles/10429518.html

go包管理:http://www.imooc.com/article/74033

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值