You build Go code from a .proto file by installing a Protocol Buffers compiler, installing the Go code-generation plugins, running the compiler with a go_out target (or letting buf generate do it), and then importing the generated package into your module. The key condition is version matching: the generator plugin and the google.golang.org/protobuf runtime must be on compatible releases, and the generated files must sit inside your Go module so the import paths resolve.

That is the whole answer in miniature. The rest of this article is the operational detail behind it—prerequisites, the exact commands, how output paths and import paths interact, what breaks on real projects, and when a second build system like buf or go generate earns its place.

I'll assume Go modules and a current protobuf release line, and I'll point out where older github.com/golang/protobuf setups differ. I won't guess at specific version numbers, because plugin releases move and any number I invent would be stale by the time you read it.

Prerequisites: The Compiler, the Plugins, and a Go Module

Three separate pieces have to be present, and people often confuse them.

The compiler (protoc) is a standalone binary that parses .proto files and produces a language-neutral description. It does not generate Go by itself. Install it from your operating system's package manager (for example, apt install protobuf-compiler or brew install protobuf) or from the official release archives. Check it responds:

The Go generator plugins are separate binaries: protoc-gen-go for message types, and protoc-gen-go-grpc if you also want gRPC service stubs. Install them with go install:

The protoc-gen- prefix is not decoration. When you pass --go_out, protoc looks for an executable named protoc-gen-go on your PATH. If it can't find it, you get a confusing error instead of a missing-file error.

A Go module for the generated code to live in. If you don't have one, go mod init example.com/myapp creates it. The go_package option in your proto (covered below) must agree with this module path.

One practical gotcha: go install puts binaries in $(go env GOPATH)/bin, which is often not on PATH by default. Add it, then confirm protoc-gen-go --version runs. If the shell says "command not found" while protoc itself works, that's the cause.

Writing the Proto So the Go Output Has a Home

Before generating anything, the contract for where Go code goes lives inside the .proto file itself—specifically in the go_package option.

The go_package value has two parts separated by a semicolon: the import path (example.com/myapp/gen/user/v1) and, optionally, the Go package name (userv1). Without an explicit package name, protoc-gen-go derives one from the last path segment, which can produce awkward identifiers.

This option is not cosmetic. Modern protoc-gen-go refuses to run if go_package is missing on any file being compiled, because it has no way to know the import path. If you're working in an existing repository that predates this requirement, you'll see errors like "unable to determine Go import path"—the fix is adding the option, not downgrading the plugin.

A few conventions worth adopting early:

Namespace with a version segment (user/v1) so future breaking changes get a new package instead of mutating the old one. Keep generated output in a dedicated directory (commonly gen/ or api/) that's clearly not hand-edited. Put the go_package path in the same module you'll import from, or a separate module you publish—just be consistent.

Running protoc and Reading the Result

With the proto written and the plugins installed, the generation command looks like this:

Walking through it:

--proto_path=proto (also written -I proto) tells the compiler where to resolve imports from. If your file imports google/protobuf/timestamp.proto, the well-known types ship with protoc and are found automatically, but your own imports are resolved relative to the include paths you list.

--go_out=gen is the output root directory.

--go_opt=paths=source_relative controls how the output path is computed inside gen. This flag matters more than it looks. Without it, the default behavior uses the go_package import path to build the directory structure, which can scatter files across nested folders you didn't expect. With source_relative, a file at proto/user/v1/user.proto becomes gen/user/v1/user.pb.go—predictable and easy to wire into a Makefile.

The result is a user.pb.go file containing your message structs, getters, and the descriptor registration code. If you also defined a service and run the gRPC plugin:

you additionally get user_grpc.pb.go with the client and server interfaces.

How to check success: the command exits with status 0, the expected .pb.go files exist, and go build ./... from the module root compiles without complaints about the generated package.

Wiring Generated Code Into Your Program

Once gen/user/v1/user.pb.go exists inside your module, you just import it:

The runtime dependency is google.golang.org/protobuf, pulled in automatically the first time you build. Run go mod tidy after adding generated code so the module graph updates and go.sum records the checksums.

Two behavioral notes that trip people up:

Generated code is not meant to be edited. Every regeneration overwrites it. Put a clear comment or a .gitattributes marking on the directory if your team needs the reminder.

Getters are defensive, direct field access is not. user.GetEmail() returns an empty string on a nil receiver, while user.Email panics. In code that handles untrusted or partially populated messages, the getter form is the safer default.

Paths, Packages, and the Errors That Actually Happen

Most failures in this workflow are path problems or version problems, and each has a specific signature.

"unable to determine Go import path" means a proto file lacks go_package. Add it. If you're compiling third-party protos you don't control, you can supply the mapping on the command line with --go_opt=Mproto/foo/foo.proto=example.com/gen/foo instead of editing their files.

"protoc-gen-go: program not found or is not executable" means the plugin binary isn't on PATH. Recheck the GOPATH/bin situation described earlier.

Files land in the wrong directory usually means paths=source_relative was omitted, or the --proto_path you passed doesn't match the relative path you gave the compiler. Those two must be consistent: if you include proto as a path, the file argument should be proto/user/v1/user.proto, not ../proto/user/v1/user.proto.

Compile errors about an API mismatch ("X does not implement proto.Message" or missing generated methods) point at a runtime mismatch. If a dependency still imports the old github.com/golang/protobuf module and you've generated with the newer plugin, you can get incompatibility. The github.com/golang/protobuf package was reimplemented as a thin wrapper over google.golang.org/protobuf, so upgrading the runtime in your go.mod and regenerating is usually the clean fix. Pin your plugin version in a tools.go file or a script so everyone on the team generates identical output.

A quick diagnostic habit: run protoc with --descriptor_set_out=/dev/null plus only the include paths. If that fails, the problem is in the proto or the imports, not the Go plugin.

When buf Is the Better Tool

protoc works, but it makes you manage include paths, plugin installation, and option flags by hand. buf is a modern alternative that wraps the same compilers with configuration files and dependency management.

A minimal setup starts with buf.yaml at the repo root declaring the module, and a buf.gen.yaml describing the plugins:

Then buf generate produces the same output as the manual protoc invocation. The local: key means "use the plugin on my PATH"; remote: pulls plugins from the Buf Schema Registry, which removes the installation step from every developer machine and CI runner entirely.

The trade-offs are real and worth naming. buf adds a tool and two config files to learn. In exchange you get:

Linting (buf lint) that enforces naming and layout conventions, catching problems before they reach generated code. Breaking-change detection (buf breaking --against) that compares your current schema to a previous version, which matters once a proto is deployed. Deterministic dependency resolution via the schema registry, instead of vendoring third-party protos by hand.

For a single-person project with one proto file, protoc is fine and one less thing to install. For a team or a service with multiple proto modules, the manual path management becomes a recurring source of the errors described in the previous section, and buf earns its overhead. That's a judgment call based on scale, not a rule.

Keeping Generated Code Current

Regeneration has to happen whenever protos change, and the failure mode when it doesn't is subtle: your code compiles against stale generated types, and the mismatch surfaces later as a missing field or a wire-format surprise.

go generate. Add a directive comment in a regular Go file that imports nothing:

Then go generate ./... reruns it. This keeps the command versioned next to the code that depends on it, which is the main benefit over a shell script nobody remembers to run.

Makefile or task target. A make proto target that invokes buf generate (or the full protoc line) is the most common arrangement. Have CI run it and check that git diff --exit-code is clean afterward—that single check catches the "forgot to regenerate" case permanently.

Committing versus ignoring generated output. Committing .pb.go files makes the build work for anyone who clones the repo without the toolchain, and it makes diffs reviewable. Ignoring them keeps the repo smaller but forces every consumer to install protoc and the plugins. The protobuf project's own guidance has leaned toward committing generated code in many ecosystems; for Go services, either choice is defensible as long as it's consistent. If you commit, add a CI step that regenerates and fails on a non-empty diff, so the committed files can't silently drift.

Version pinning matters more than people expect. go install ...@latest is convenient for a first run and a liability for reproducibility, because two developers can generate different output from the same proto. Pin tool versions in your tools.go (build-tagged imports of the plugin packages) and in buf.gen.yaml, and treat a plugin upgrade as a deliberate change you review.

Worked Example and a Sanity Check

Suppose your module is example.com/shop, and you have proto/cart/v1/cart.proto with:

and a message Cart with a repeated Item items field. After running:

you should find gen/cart/v1/cart.pb.go. Inside it, the struct will have a field like Items []*Item with a GetItems() method. From example.com/shop you can import cartv1 "example.com/shop/gen/cart/v1" and instantiate &cartv1.Cart{Items: []*cartv1.Item{{Sku: "A1"}}}.

If go build then complains that the package doesn't exist despite the directory being present, the go_package string and the directory layout disagree with the module path. That's the check to run: the import path you write in Go must equal both go_package and the module path plus the directory under it. When all three line up, the build succeeds.

Stop condition: if you're generating code for a proto that governs a live wire protocol between services and you're unsure whether a change is backward compatible, don't rely on "it compiles." Use buf breaking against the deployed version, or get a second reviewer with knowledge of the protocol's evolution. Generation is mechanical; schema compatibility is a design decision.

Frequently Asked Questions

Do I need the gRPC plugin if I only use messages?

No. protoc-gen-go alone produces message types, serialization, and reflection support. Add protoc-gen-go-grpc only when your proto declares service blocks and you want client/server stubs.

Can I generate Go without protoc installed?

Yes, if you use buf with remote plugins—buf generate can run the compiler and plugins without a local protoc binary. The protoc route requires the binary on every machine that regenerates.

Why does my generated file have a protoimpl package import I didn't write?

That's the modern runtime's internals registering your message types. It's expected, not a sign of a bad generation. Don't edit it; regenerate from a corrected proto instead.

Should generated files be in the same module as my application?

Either works. Keeping them in a separate module lets you version the API contract independently and avoids recompiling everything on each proto change—useful at scale, extra ceremony for a small project.