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

Golang · Fecshop · 于 3个月前 发布 · 178 次阅读

我的环境是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,成功。

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics