Skip to content

Today I learned

Learn something new every day

Menu
  • Home
  • About
  • Hello World! and Why I Started This Blog
Menu

Don’t Make, Go Generate It

Posted on 28/11/202224/11/2022 by Sagi Rosenthal

Today I learned why and how to go generate. A small replacement for makefile I already wrote of. But what are the differences? Why use it, and also important- when not to. And a little more technical information you might need.

go generate clause

As part of developing an actual project, you sometimes need to generate files. You know… the ones with # generated file DO NOT EDIT comment on them. one example can be protobuf files, you have the recipe data structure in .proto and you want to convert it to an import-able file type. So you probably do a small makefile like this

proto:
    protoc -I=pkg/protos --go_out=pkg/protos pkg/protos*.proto

But then time flies and the makefile becomes big and complex, nobody knows what or why it is needed and which resources are using it.

This is when go generate comes to solve- to help you generate utilities for your project in an easy way. Lets say you have a webserver using gRPC and you need protobuf to generate the methods and data types. You can add this code bloc to your server implementation file:

//go:generate protoc -I=pkg/protos --go_out=pkg/protos pkg/protos*.proto

and whenever you want to rebuild the .pb.go file you can simply run go generate ./... and it will run it. here are some few more tips and good-to knows:

  • the go:generate comment should be at the beginning of the file and can run any single bash command
  • yes, this is also include go run command. you can write you utilities as go script
  • go build does NOT run go generate before it compiles your code
  • you cannot run 2 commands in the same //go:generate clause. but you can write multiple of them and they will be executed one by one
makefile vs go generate bell curve

So when to use go generate? well, for the obvious reason- if you have files and utilities that needs to be generated. protobufs, automatic code generation like handlers, and non-go depandencies. This can also be a good usage for other cases if you run a small go project that doesn’t have a makefile
When not? if you already have A makefile. if you have a complex usecases that needs to delete stuff and validate things and use variable… you know, the reson you whould use a makefile. But even when you do, sometimes breaking little tasks to go generate can help you document it better.

Further reading:
Generating code – from go.dev blog

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Recent Posts

  • How to Sell Stuff Online
  • How to Make Your Kubernetes Dev Cycle Faster
  • How To Be A Valuable Team Member
  • Don’t Make, Go Generate It
  • Why You Should Monkey Patch Your Next Unit-Test

Archives

  • July 2023
  • April 2023
  • January 2023
  • November 2022
  • August 2022
  • July 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Go
  • Learning
  • Software Utilities
Join the updates channel
© 2026 Today I learned | Powered by Minimalist Blog WordPress Theme