🐹 Go (Golang) Intermediate

What is go generate in Go?

Answer

go generate scans Go source files for special comments of the form //go:generate command args and runs those commands when you execute go generate ./.... It is not part of the regular build — you run it manually and commit the generated output. Common uses include generating mock implementations from interfaces (e.g., mockgen), generating string methods for enum types (e.g., stringer), generating protobuf Go code from .proto files, and generating boilerplate from templates. The generated files are real Go source that the compiler then uses normally.