protobuf
protobuf copied to clipboard
devsite: update tutorial for specifying the Go import path
I cannot generate code, even though I have followed the tutorial correctly
the result is always: protoc-gen-go: unable to determine Go import path for "user/user.proto"
https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
As the error says, you need to specify the import path of the Go package containing the .proto
file. The simplest way to do this is with a go_package
option:
option go_package = "example.com/package/name";
(In the case of the screenshot above, the package name will be the module name in the go.mod
plus "/user"
.)
Changing this issue to be about documentation. The tutorial needs to be updated.
I previously had this on my proto file:
option go_package = ".;proto";
Changing it to something like "example.com/package/name"
has the inconvenient effect of having
protoc -Iproto proto/test.proto --go_out=/tmp/proto
output the code to /tmp/proto/example.com/package/name/test.pb.go
.
Is there a way to output the code /tmp/proto/test.pb.go
?
Thanks
Update: I think this works:
option go_package = "/;proto";
Is there a way to output the code /tmp/proto/test.pb.go?
Pass --go_opt=paths=source_relative
to protoc
.
See: https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation
Thanks, @neild, it's a cleaner way.
However, it looks like this option doesn't apply to --go-grpc_out
; tested with:
option go_package = "example.com/package/name";
and
protoc -Iproto proto/test.proto --go_opt=paths=source_relative --go_out=foo --go-grpc_out=foo
If I am not missing anything, maybe I should open an issue on https://github.com/grpc/grpc-go.
You need to provide the option to both plugins: --go-grpc_opt=paths=source_relative
.
Makes sense, thanks again!
I tried passing --go_opt=paths=source_relative
to protoc
but got Unknown flag: --go_opt
. Has anyone else encountered this?
That means that the version of the protobuf toolchain is too old. I'm not sure when the "_opt" feature was introduced, but you can find the latest releases here: https://github.com/protocolbuffers/protobuf/releases
Alternatively, I you can fold the options into the "go_out" flag:
--go_out=paths=source_relative:$OUT_DIR
@neild Thanks . it just made my day sir
Thanks, @neild, it's a cleaner way.
However, it looks like this option doesn't apply to
--go-grpc_out
; tested with:option go_package = "example.com/package/name";
and
protoc -Iproto proto/test.proto --go_opt=paths=source_relative --go_out=foo --go-grpc_out=foo
If I am not missing anything, maybe I should open an issue on https://github.com/grpc/grpc-go.
How to define the path to import the package in the client/server code?
I think --go_opt=paths=source_relative
should be the default.
I think --go_opt=paths=source_relative should be the default.
Unfortunately impossible to change without breaking existing users.
Also, I strongly suspect --go_opt=module=<module>
is better for most cases these days.
You right! I tried it now, it's working really well.
Can we close this issue?
The devsite tutorial probably still needs to be updated. I'll look into it early next week.