Golang项目执行go get命令遇github.com/golang/protobuf模块弃用问题求助
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/protowithgoogle.golang.org/protobuf/proto - Replace
github.com/golang/protobuf/jsonpbwithgoogle.golang.org/protobuf/encoding/protojson - Replace
github.com/golang/protobuf/ptypeswithgoogle.golang.org/protobuf/types/known(note: subpackages likeanypbortimestamppblive underknownnow) - 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.modandgo.sumfiles, removing unused old dependencies and adding the new ones:go mod tidy - Verify the new module is now in your
go.modby running:
You should see a version number (e.g.,go list -m google.golang.org/protobufgoogle.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 runningwhich protoc-gen-go(it should point to your$GOPATH/bindirectory). If not, add$GOPATH/binto yourPATHenvironment 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

