protocol 在goland中的使用

问题:

最近在看go语言高级编程。在看到 4.6 (gRPC和Protobuf扩展) 的时候遇到一个问题。

import "github.com/mwitkow/go-proto-validators/validator.proto";

这句话在goland中爆红,且运行时不被识别。

解决:

解决改问题需要两个步骤

注意:在根据文档进行安装的时候,go get可能回报错,根据提示使用go install即可。现在的golang提倡使用go install,在使用go get的时候可以增加 -d 。

1.goland setting

点击goland左上角的 file —> setting —> protocol buffers

这是goland设置protocol 位置的地方,其中有一项名为:  configure automatically 这个选择是自动配置protocol的路径

它默认的位置是

file://C:/Users/username/AppData/Local/JetBrains/GoLand2021.3/protoeditor

username 表示你所用的电脑的用户的名称

而我们使用go install下载的时候,位置是 %GOPATH%/pkg/mod 所以我们需要 取消configure automatically,然后点击 “+” ,增加 %GOPATH%/pkg/mod 路径。以防万一,我在这里添加的绝对路径。

2. cmd/ps 运行

        还有个改动是,我将上方中的import,改为了这句话:

import "github.com/mwitkow/[email protected]/validator.proto";
protoc  \
    --proto_path=C:\Users\username\AppData\Local\JetBrains\GoLand2021.3\protoeditor\google   \
    --proto_path=D:\GoENV\gopath\pkg\mod   \
    --proto_path=. --govalidators_out=.   \
    --go_out=plugins=grpc:.  hello.proto

username 表示你所用的电脑的用户的名称

D:\GoENV\gopath\pkg\mod : 是 %GOPATH%/pkg/mod 路径

至此,结束

你可能感兴趣的:(golang学习,golang,开发语言,go语言高级编程)