安装Gin框架出现依赖包google.golang.org/protobuf无法安装的解决方法

一、这里默认你已经安装了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安装成功。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值