You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go语言GRPC网关生成报错:protoc-gen-grpc-gateway未找到或不可执行

Go gRPC网关配置问题及解决方法

我曾查找过类似帖子,但它们年代久远且未能解决我的问题。我正尝试为Go语言gRPC服务添加网关,使用的Makefile命令如下:

PROJ_PATH=${CURDIR}

.PHONY: proto
proto: ## Generate protobuf code
# Compile proto files inside the project.
    protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=. \
            --grpc-gateway_out . \
            --grpc-gateway_opt generate_unbound_methods=true \
            --openapiv2_out . \
            --openapiv2_opt logtostderr=true \
            --openapiv2_opt generate_unbound_methods=true

我已添加以下两个依赖:

go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2

但仍报错:

protoc-gen-grpc-gateway: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--grpc-gateway_out: protoc-gen-grpc-gateway: Plugin failed with status code 1.
make: *** [proto] Error 1

此外,我还遇到了protobuf文件导入错误,不得不移除相关导入和网关配置:

错误信息:

google/api/annotations.proto: File not found.
api.proto:5:1: Import "google/api/annotations.proto" was not found or had errors.
make: *** [proto] Error 1

修改前的proto文件:

syntax = "proto3";

option go_package = "pkg/api";

import "google/api/annotations.proto";  <---- 不得不移除这行

service ApiService {
    rpc Test(TestRequest) returns (TestResponse){

        option (google.api.http) = {  <---- 不得不移除这部分
            get: "/v1/test"
            body: "*"
        };
    }
}

最终通过以下方式解决:

  • 执行安装命令,将插件安装到GOPATH的bin目录:
go install \
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest \
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
  • 在proto命令中添加插件的绝对路径参数:
--plugin=protoc-gen-grpc-gateway=${GOPATH}/bin/protoc-gen-grpc-gateway \

完整的protoc命令如下:

protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=. \
       --grpc-gateway_out . \
       --grpc-gateway_opt generate_unbound_methods=true \
       --plugin=protoc-gen-grpc-gateway=${GOPATH}/bin/protoc-gen-grpc-gateway \
       --openapiv2_out . \
       --openapiv2_opt logtostderr=true \
       --openapiv2_opt generate_unbound_methods=true

内容的提问来源于stack exchange,提问作者Mike3355

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 05:01:25