centos6 安装go框架gin的步骤,以及中间遇到的坑

我的环境是centos 6,安装go语言的文档参看:

centos6 – 安装 golang 1.9

安装完go语言后,安装gin框架,github地址为:https://github.com/gin-gonic/gin

通过readme的介绍,通过命令行安装:

go get github.com/gin-gonic/gin

结果发现,一直卡住,无法通过go get完成安装,最后查阅了资料发现是git版本的问题,参看:https://github.com/go-playground/validator/issues/224#issuecomment-165324546

然后我重新安装了git,参看文档:

centos6 安装 git2.7

然后,重新执行安装

[root@iZ942k2d5ezZ go]# go get -v  github.com/gin-gonic/gin

github.com/gin-gonic/gin (download)
github.com/gin-contrib/sse (download)
github.com/golang/protobuf (download)
github.com/ugorji/go (download)
Fetching https://gopkg.in/go-playground/validator.v8?go-get=1
Parsing meta tags from https://gopkg.in/go-playground/validator.v8?go-get=1 (status code 200)
get "gopkg.in/go-playground/validator.v8": found meta tag get.metaImport{Prefix:"gopkg.in/go-playground/validator.v8", VCS:"git", RepoRoot:"https://gopkg.in/go-playground/validator.v8"} at https://gopkg.in/go-playground/validator.v8?go-get=1
gopkg.in/go-playground/validator.v8 (download)
Fetching https://gopkg.in/yaml.v2?go-get=1
Parsing meta tags from https://gopkg.in/yaml.v2?go-get=1 (status code 200)
get "gopkg.in/yaml.v2": found meta tag get.metaImport{Prefix:"gopkg.in/yaml.v2", VCS:"git", RepoRoot:"https://gopkg.in/yaml.v2"} at https://gopkg.in/yaml.v2?go-get=1
gopkg.in/yaml.v2 (download)
github.com/mattn/go-isatty (download)
github.com/gin-contrib/sse
github.com/gin-gonic/gin/json
github.com/golang/protobuf/proto
github.com/ugorji/go/codec
gopkg.in/go-playground/validator.v8
github.com/gin-gonic/gin/binding
gopkg.in/yaml.v2
github.com/gin-gonic/gin/render
github.com/mattn/go-isatty
github.com/gin-gonic/gin
[root@iZ942k2d5ezZ go]# 

然后,创建g3.go文件,内容如下:

package main

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

func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.Run("120.24.37.249:3000") // listen and serve on 0.0.0.0:8080
}

注意:

1.ip换成您自己的ip

2.iptables 开放3000端口

/sbin/iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
/etc/rc.d/init.d/iptables save

 

执行Log

[root@iZ942k2d5ezZ golang]# go run g3.go 

[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] GET    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

访问:http://120.24.37.249:3000/ping

页面返回:

{"message":"pong"}

 

OK,成功。

 

 

 

centos6 – 安装 golang 1.9

1.下载安装包,地址为:https://golang.org/dl/ , 我的操作系统为centos6,我下载的是:go1.9.2.linux-amd64.tar.gz

cd /tools
wget https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
tar -C /usr/local -xzf /usr/local/go1.9.2.linux-amd64.tar.gz

2.写入环境变量

如果 /root/go 文件夹不存在,则新建该文件夹

vim /etc/profile ,在最后另起新行添加下面的环境变量

export GOROOT=/usr/local/go
export GOPATH=/root/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

让环境变量生效:

source /etc/profile

这样,go安装就完成了。

 

 

 

centos6 安装 git2.7

在使用 go get 安装gin框架的时候,遇到了坑,centos 6 默认的git是1.7.1,导致安装一直卡住,后来查阅文章:https://github.com/go-playground/validator/issues/224 ,

发现,是centos自带的git版本太低的问题导致的,于是,卸掉git ,重新安装2.7.2

卸载Centos自带的git1.7.1
通过git –version查看系统带的版本,Cento6.5应该自带的是git版本是1.7.1

yum remove git

安装依赖:

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install gcc perl-ExtUtils-MakeMaker

下载git最新版本

cd /tools
wget https://www.kernel.org/pub/software/scm/git/git-2.7.2.tar.gz
tar xzf git-2.7.2.tar.gz

 

安装git,然后将其添加到环境变量,命令操作如下:

cd git-2.7.2
make prefix=/usr/local/git all
make prefix=/usr/local/git install
vim /etc/profile
// 在末尾新开一行填写下面的代码
export PATH=$PATH:/usr/local/git/bin
// :wq保存退出,然后执行下面的命令,让其生效
source /etc/profile

 

到这里就安装成功了,查看git版本号
git –version
git version 2.7.2