windows下go安装并使用protobuf
创始人
2024-05-29 01:29:05

go使用protobuf的过程以及可能出现的问题

  • 1. 下载proto windows版本并安装
  • 2. 安装protoc-gen-go
  • 3. proto文件转go文件报错protoc-gen-go: unable to determine go import path for "xxxx.proto"
  • 4. 生成的go文件中google.golang.org/protobuf/reflect/protoreflect依赖爆红

1. 下载proto windows版本并安装

官方地址:protobuf
解压后复制bin目录下的protoc.exe到GOPATH\bib目录中
在这里插入图片描述

2. 安装protoc-gen-go

如果使用go get -u github.com/golang/protobuf/protoc-gen-go 可能会出现如下错误:

go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.go get: installing executables with 'go get' in module mode is deprecated. Use 'go install pkg@version' instead.For more information, see https://golang.org/doc/go-get-install-deprecation or run 'go help get' or 'go help install'.

使用下面的命令进行下载
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
安装成功后,在GOPATH\bib目录中可以看到proroc-gen-go.exe

两个全部安装成功后,查看下版本号
在这里插入图片描述

3. proto文件转go文件报错protoc-gen-go: unable to determine go import path for “xxxx.proto”

需要注意的是在proto文件中,记得设置option go_package属性

syntax="proto3"; //版本号
package protobuf;  //包名
option go_package ="./protobuf";enum ClassName{   //枚举class1=0;  //标号 必须从 0开始class2=1;class3=2;
}message Student{ //消息,对应于Go的结构体string name=1; //1:标号,唯一 即可(相当于数据库中的Id,不一定要从1 ,2的顺序依次排列。)int32 age=2;  //必须指定整型的范围,如int32,int64string address=3;ClassName cn=4;
}
message Students{repeated Student person=1;  // repeated 修饰,相当于Go中切片string school=2;
}

再使用protoc --go_out=. test.proto命令生成go文件
在这里插入图片描述

在这里插入图片描述

4. 生成的go文件中google.golang.org/protobuf/reflect/protoreflect依赖爆红

import(
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
)

在GOPATH/src目录下新建google.golang.org文件夹,在该文件下使用命令:
git clone https://e.coding.net/robinqiwei/googleprotobuf.git protobuf

在这里插入图片描述
重新保存生成的xxx.pb.go文件,爆红消失
在这里插入图片描述

相关内容

热门资讯

玩家必看教程!智星德州扑克辅助... 玩家必看教程!智星德州扑克辅助挂多功能透视工具,好像真的是有挂(有挂详细)1、该软件可以轻松地帮助玩...
记者揭秘!wpk辅助挂多功能透... 记者揭秘!wpk辅助挂多功能透视工具,原来存在有挂(有挂猫腻)1、全新机制【wpkai辅助工具激活码...
科普!德扑圈辅助挂多功能透视工... 科普!德扑圈辅助挂多功能透视工具,总是有挂(有挂教学)1、实时德扑圈透视辅助更新:用户可以随时随地访...
玩家必备攻略!WPK辅助挂多功... 玩家必备攻略!WPK辅助挂多功能透视工具,其实存在有挂(确实有挂)1、任何WPK透视是真的假的的玩家...
分享一款!智星菠萝德州辅助挂多... 分享一款!智星菠萝德州辅助挂多功能透视工具,总是是真的有挂(有挂神器)1)智星菠萝德州辅助插件:进一...