make proto in micro project throws error Expected "{" in windows 10

huangapple go评论100阅读模式
英文:

make proto in micro project throws error Expected "{" in windows 10

问题

micro开始,一切都正常工作。我没有VisualStudio,但我通过chocolatey安装了make(不知道是否可能是问题)。

我使用以下命令引导服务:

  1. λ micro new my.test
  2. Creating service my.test
  3. .
  4. ├── micro.mu
  5. ├── main.go
  6. ├── generate.go
  7. ├── handler
  8. └── my.test.go
  9. ├── proto
  10. └── my.test.proto
  11. ├── Dockerfile
  12. ├── Makefile
  13. ├── README.md
  14. ├── .gitignore
  15. └── go.mod

下载protoc zip包(protoc-$VERSION-$PLATFORM.zip)并安装:

访问https://github.com/protocolbuffers/protobuf/releases

下载用于micro的protobuf:

  1. go get -u github.com/golang/protobuf/proto
  2. go get -u github.com/golang/protobuf/protoc-gen-go
  3. go get github.com/micro/micro/v3/cmd/protoc-gen-micro

编译proto文件my.test.proto:

  1. cd my.test
  2. make proto

安装依赖项,一切都正常。然后我进入my.test目录,在运行make proto之后出现了以下错误:

  1. protoc --proto_path=. --micro_out=. --go_out=:. proto/proto.test.proto
  2. proto/proto.test.proto:7:14: Expected "{"
  3. make: *** [Makefile:16: proto] Error 1

我已经安装了所有依赖项,并且我的PATH设置正确,但我不知道问题出在哪里。

编辑:这是我的proto文件,由micro方便地生成:

  1. syntax = "proto3";
  2. package proto.test;
  3. option go_package = "./proto;proto.test";
  4. service Proto.Test {
  5. rpc Call(Request) returns (Response) {}
  6. rpc Stream(StreamingRequest) returns (stream StreamingResponse) {}
  7. rpc PingPong(stream Ping) returns (stream Pong) {}
  8. }
  9. message Message {
  10. string say = 1;
  11. }
  12. message Request {
  13. string name = 1;
  14. }
  15. message Response {
  16. string msg = 1;
  17. }
  18. message StreamingRequest {
  19. int64 count = 1;
  20. }
  21. message StreamingResponse {
  22. int64 count = 1;
  23. }
  24. message Ping {
  25. int64 stroke = 1;
  26. }
  27. message Pong {
  28. int64 stroke = 1;
  29. }
英文:

Starting with micro and everything works fine. I dont have VisualStudio, but instead I have make through chocolatey(dont know if could be the issue)

I bootstrap the service with micro new like this.

  1. λ micro new my.test
  2. Creating service my.test
  3. .
  4. ├── micro.mu
  5. ├── main.go
  6. ├── generate.go
  7. ├── handler
  8. │   └── my.test.go
  9. ├── proto
  10. │   └── my.test.proto
  11. ├── Dockerfile
  12. ├── Makefile
  13. ├── README.md
  14. ├── .gitignore
  15. └── go.mod
  16. download protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:
  17. visit https://github.com/protocolbuffers/protobuf/releases
  18. download protobuf for micro:
  19. go get -u github.com/golang/protobuf/proto
  20. go get -u github.com/golang/protobuf/protoc-gen-go
  21. go get github.com/micro/micro/v3/cmd/protoc-gen-micro
  22. compile the proto file my.test.proto:
  23. cd my.test
  24. make proto

Installed dependencies and everything was fine. Then I moved to my.test and after make proto this error shows up

  1. protoc --proto_path=. --micro_out=. --go_out=:. proto/proto.test.proto
  2. proto/proto.test.proto:7:14: Expected "{".
  3. make: *** [Makefile:16: proto] Error 1

I have all dependencies and my PATH is fine, but I dnt know what the problem is.

Edit: This is my proto, conveniently generated by micro

  1. syntax = "proto3";
  2. package proto.test;
  3. option go_package = "./proto;proto.test";
  4. service Proto.Test {
  5. rpc Call(Request) returns (Response) {}
  6. rpc Stream(StreamingRequest) returns (stream StreamingResponse) {}
  7. rpc PingPong(stream Ping) returns (stream Pong) {}
  8. }
  9. message Message {
  10. string say = 1;
  11. }
  12. message Request {
  13. string name = 1;
  14. }
  15. message Response {
  16. string msg = 1;
  17. }
  18. message StreamingRequest {
  19. int64 count = 1;
  20. }
  21. message StreamingResponse {
  22. int64 count = 1;
  23. }
  24. message Ping {
  25. int64 stroke = 1;
  26. }
  27. message Pong {
  28. int64 stroke = 1;
  29. }

答案1

得分: 2

Go包的名称不能包含点号 - 这样只会导致编译错误:

  1. $ cat pkg.go
  2. package dot.test
  3. $ go build
  4. ./pkg.go:1:12: 语法错误: 意外的 .

因此,必须确保生成的代码产生一个有效的Go包名称。

根据Go规范中的package clause的定义:

> 包子句在每个源文件中开始,并定义文件所属的包。

  1. PackageClause = "package" PackageName .
  2. PackageName = identifier .

以及identifier的定义为:

> ... 一个或多个字母和数字的序列。标识符中的第一个字符必须是字母。

  1. identifier = letter { letter | unicode_digit } .
英文:

Go package names cannot contain dots - it will just not compile:

  1. $ cat pkg.go
  2. package dot.test
  3. $ go build
  4. ./pkg.go:1:12: syntax error: unexpected .

so one has to ensure generated code produces a valid Go package name.


From the Go spec, the package clause is defined as:

> A package clause begins each source file and defines the package to
> which the file belongs.

  1. PackageClause = "package" PackageName .
  2. PackageName = identifier .

and an identifier as:

> ... a sequence of one or more letters and digits. The first
> character in an identifier must be a letter.

  1. identifier = letter { letter | unicode_digit } .

答案2

得分: 0

在阅读了一些基础知识后,我找到了问题所在。即使proto文件是由微服务生成的,第七行的代码service Proto.Test是问题所在,因为有一个点。我的意思是,将其替换为service Test解决了这个问题。现在我不知道为什么。如果能给出任何解释,我将不胜感激。顺便说一下,我在使用Windows。

英文:

After some reading of the basics I found the problem. Even if the proto file is generated by micro this code service Proto.Test at line seven is the problem, because of the dot. I mean, replacing that for service Test solved the issue. Now I dont know why. Any explanation would be preciated. I am in windows by the way

huangapple
  • 本文由 发表于 2021年9月11日 04:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/69137632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定