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

Golang项目执行go get命令遇github.com/golang/protobuf模块弃用问题求助

Fixing the "github.com/golang/protobuf is deprecated" Error in Go Projects

Hey there, let’s work through this protobuf deprecation issue you’re facing—it’s a common roadblock when moving to the official, maintained protobuf module for Go, so let’s break down exactly how to resolve it, even if your first attempt didn’t stick.

Step 1: Update Import Paths in Your Code

The core issue is that your project (or its dependencies) is still referencing the old deprecated github.com/golang/protobuf module. Start by auditing all your Go files and updating every import line that points to the old repo:

  • Replace github.com/golang/protobuf/proto with google.golang.org/protobuf/proto
  • Replace github.com/golang/protobuf/jsonpb with google.golang.org/protobuf/encoding/protojson
  • Replace github.com/golang/protobuf/ptypes with google.golang.org/protobuf/types/known (note: subpackages like anypb or timestamppb live under known now)
  • For other subpackages, map them to the equivalent in google.golang.org/protobuf—most follow a straightforward renaming pattern.

Step 2: Clean Up Old Dependencies and Refresh Go Modules

Old cached dependencies can stick around and cause conflicts. Run these commands to clear them out and pull the new module:

  • First, clear the module cache:
    go clean -modcache
    
  • Then, let Go tidy up your go.mod and go.sum files, removing unused old dependencies and adding the new ones:
    go mod tidy
    
  • Verify the new module is now in your go.mod by running:
    go list -m google.golang.org/protobuf
    
    You should see a version number (e.g., google.golang.org/protobuf v1.31.0) printed if it’s installed correctly.

Step 3: Install the Updated protoc-gen-go Plugin

If you use Protocol Buffers to generate Go code, the old protoc-gen-go plugin from the deprecated repo is no longer supported. You need to replace it with the official one:

  • Uninstall the old plugin first:
    go uninstall github.com/golang/protobuf/protoc-gen-go
    
  • Install the latest version of the official plugin:
    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    
  • Make sure the plugin is in your system PATH—you can check by running which protoc-gen-go (it should point to your $GOPATH/bin directory). If not, add $GOPATH/bin to your PATH environment variable.

Step 4: Regenerate Your Protobuf Go Code

If you have existing .proto files that were generated with the old plugin, you need to regenerate them using the new plugin to ensure they use the google.golang.org/protobuf module:

protoc --go_out=. your_proto_file.proto

Replace your_proto_file.proto with the path to your actual proto file (or use a wildcard like *.proto to regenerate all at once).

Step 5: Test the Fix

Now run the command that was giving you the error originally:

go get .

Or try building your project:

go build

If everything worked, the deprecation warning should be gone.

What If Third-Party Dependencies Still Use the Old Module?

Sometimes, even after updating your code, a third-party dependency might still reference the deprecated github.com/golang/protobuf. In most cases, Go’s module system will handle this automatically (it can coexist with the new module), but if you run into conflicts, you can add a replace directive to your go.mod to force the dependency to use the new module:

replace github.com/golang/protobuf => google.golang.org/protobuf v1.31.0

Adjust the version number to match the one you installed in Step 2.

If you still hit issues after these steps, feel free to share more details—like your Go version, snippets of your go.mod or proto files, or any new error messages you get—and I can help troubleshoot further.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 15:27:42