protobuf使用import导入包找不到

前言

使用protobuf生成go代码,发现protobuf中一个import引用找不到

protobuf代码

syntax = "proto3";

package main;

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

message Message {
    string important_string = 1 [
        (validator.field) = {regex: "^[a-z]{2,5}$"}
    ];
    int32 age = 2 [
        (validator.field) = {int_gt: 0, int_lt: 100}
    ];
}

生成的时候报错

$ protoc --govalidators_out=. --go_out=plugins=grpc:. hello.proto
github.com/mwitkow/go-proto-validators/validator.proto: File not found.
hello.proto:5:1: Import "github.com/mwitkow/go-proto-validators/validator.proto" was not found or had errors.

解决方案

我们要弄明白protoc中proto_path参数的含义

  • proto_path: 指定了在哪个目录中搜索import中导入的和要编译为.go的proto文件,可以定义多个

所以添加proto_path就可以了,指定两个地址,一个是import的地址,一个是要编译为.go的proto文件的地址

$ protoc --proto_path=. --proto_path=${GOPATH}/src --govalidators_out=. --go_out=plugins=grpc:. hello.proto

相关文章:

  • 2021-08-12
  • 2022-12-23
  • 2022-01-01
  • 2021-04-15
  • 2021-10-13
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2022-01-31
  • 2021-10-20
  • 2021-05-29
  • 2021-07-06
相关资源
相似解决方案