一、这里默认你已经安装了Go,然后使用Go命令安装Gin
go get -u github.com/gin-gonic/gin
在安装gin的过程中出现 google.golang.org/protobuf安装失败,终端提示信息如下:
package google.golang.org/protobuf/encoding/prototext: unrecognized import path "google.golang.org/protobuf/encoding/prototext" (https fetch:
Get https://google.golang.org/protobuf/encoding/prototext?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because
the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to res
pond.)
package google.golang.org/protobuf/proto: unrecognized import path "google.golang.org/protobuf/proto" (https fetch: Get https://google.golang.
org/protobuf/proto?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly re
spond after a period of time, or established connection failed because connected host has failed to respond.)
package google.golang.org/protobuf/reflect/protoreflect: unrecognized import path "google.golang.org/protobuf/reflect/protoreflect" (https fet
ch: Get https://google.golang.org/protobuf/reflect/protoreflect?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed be
cause the connected party did not properly respond after a period of time, or established connection failed because connected host has failed
to respond.)
............
二、测试gin是否安装成功
测试Gin:
ginHelloWorld_test.go 代码:
package example
import (
"github.com/gin-gonic/gin"
"net/http"
"testing"
)
func TestGinHello(t *testing.T) {
// 1、创建路由
r := gin.Default()
// 2、绑定路由规则,执行的函数
// gin.Context, 封装了request 和 response
r.GET("/gin/test", func(c *gin.Context) {
c.String(http.StatusOK, "hello world!")
})
// 3、监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8081")
}
运行报错。
二、解决方法
1、将 google.golang.org/protobuf 包对应的github上的地址git或下载下来,github地址:
https://github.com/protocolbuffers/protobuf-go
2、在在Go的"$GOPATH"目录的src目录下,创建“google.golang.org/protobuf” 目录;
3、将下载或git文件的protobuf-go/目录下的全部内容复制到上面创建的google.golang.org/protobuf目录下。
git clone 的protobuf-go/ 目录下的文件:
将git clone的"protobuf-go/" 目录下的全部文件复制到 "google.golang.org/protobuf "中:
再次运行上面的测试栗子:
成功的跑了起来,访问 http://localhost:8081/gin/test
到此gin安装成功。