Skip to content

Go Proto Import

DodaTech 1 min read

In this tutorial, you'll learn about Protobuf: Imports Not Found. We cover key concepts, practical examples, and best practices.

Protobuf imports -- Configure protoc with correct import paths so proto files can reference types from other protos and well-known types.

The Problem

protoc uses -I/--proto_path to resolve imports. Missing paths cause import errors. Well-known types need the Google API path.

Wrong

syntax = "proto3";
import "google/protobuf/timestamp.proto"; // Not found!

Output:

$ protoc --go_out=. user.proto
// google/protobuf/timestamp.proto: File not found.
$ protoc -I. -I$(go list -m -f '{{.Dir}}' google.golang.org/protobuf)     --go_out=. --go-grpc_out=. user.proto
// Or with Buf:
// version: v1
// deps:
//   - buf.build/googleapis/googleapis

Output:

// Imports resolved. Code generated successfully.

Prevention

  • Use -I. to include current directory
  • Use -I with path to google/protobuf for well-known types
  • Consider using Buf CLI for dependency management
  • Use relative paths in import statements
  • Avoid circular imports between proto files

Common Mistakes with proto import

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**What is Buf?**

A protobuf build tool that manages dependencies, linting, and breaking change detection.

Where are well-known types?

In github.com/protocolbuffers/protobuf/src or google.golang.org/protobuf.

Can I import from other modules?

Yes. Add -I to the module's proto directory or use Buf.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro