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

Golang中protoc生成文件后go mod tidy报模块无指定包错误

Go模块与Protobuf生成包导入问题排查

初始问题

本人对Golang不太熟悉,可能犯了低级错误,但始终无法解决:

  • 有一个proto3文件(命名为file.proto),头部内容:
syntax = "proto3";
package [package_name];
option go_package = "github.com/[user]/[repository]";
  • 使用protoc命令生成文件:
protoc --go_out=$GOPATH/src --go-grpc_out=$GOPATH/src file.proto
  • 成功生成file.pb.go和file_grpc.pb.go文件,位于/go/src/github.com/[user]/[repository]/目录下,文件内包声明为[package_name]。
  • 代码中导入:
import (
    "github.com/[user]/[repository]/[package_name]"
)
  • 执行go mod tidy时出现错误:
go: downloading github.com/[user]/[repository] v0.0.0-20211105185458-d7aab96b7629
go: finding module for package github.com/[user]/[repository]/[package_name]
example/xxx imports
    github.com/[user]/[repository]/[package_name]: module github.com/[user]/[repository]@latest found (v0.0.0-20211105185458-d7aab96b7629), but does not contain package github.com/[user]/[repository]/[package_name]
  • 使用Go版本为go1.19 linux/amd64(基于golang:1.19-alpine镜像),尝试只导入github.com/[user]/[repository]也有同样问题。

更新操作

后续尝试以下操作:

  • 从仅含该proto文件的Git仓库获取file.proto:
wget https://raw.githubusercontent.com/[user]/[repository]/file.proto
  • 执行protoc命令生成文件:
protoc --go_out=. --go-grpc_out=. file.proto
  • 当前目录结构:
- directory
 | - process.go
 | - file.proto
 | - github.com
   | - [user]
     | - [repository]
       | - file.pb.go
       | - file_grpc.pb.go
  • 执行命令:
go mod init xxx
go mod tidy
CGO_ENABLED=0 go build process.go
  • process.go中的导入语句为:
import (
    "xxx/github.com/[user]/[repository]"
)

现在似乎能找到包,但仍出现gRPC错误,需排查是否与之前的问题相关。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 04:45:30