Talk: Code Generation with Go: Insights from GDG Hamburg Meetup

In June 2022, I had the chance to explore code generation using Go’s templating at the GDG Hamburg Meetup. It was a packed 20-minute session full of insights and practical tips. Let’s take a look back at the key points.

image

Understanding Code Generation

Code generation is all about automating the creation of code. This might seem straightforward, but it’s a powerful technique for handling repetitive tasks or ensuring consistency in a project.

Yes, as a developer, you generate code. You’re creative and smart, but manually handling repetitive tasks isn’t the most efficient use of your time. That’s where automated code generation tools come in, offering speed and consistency.

While they lack human creativity, programs are excellent at performing repetitive tasks quickly and accurately. They’re ideal for generating code based on specific rules or templates.

Code Generation in Practice

We broke down how code generation works into a simple model:

[INPUT] --> [GENERATOR] --> [OUTPUT]

Focusing on Go, we looked at generating Go code from Go code, using tools and templates to automate the process.

We discussed familiar tools like go test, openapi-generator, and Protobuf compiler that generate code from various inputs. We also mentioned tools like hugo and helm for generating static sites and Kubernetes manifests, respectively.

We took a closer look at stringer, a Go tool that automatically generates methods to satisfy the fmt.Stringer interface for integer types. This example highlighted the practical use of code generation in Go development.

The highlight of the talk was building a simple code generator together. Starting with the stringer example, we developed a generator that could automatically implement the String() method for Go types.

Key Takeaways

Looking Ahead

For anyone interested in code generation in Go or wanting to review the concepts we covered, all the code and examples from my talk are available on GitHub.

Want to learn more? Check out additional resources I recommended, like Rob Pike’s article on stringer and Paul Jolly’s talk at GopherCon UK 2019.

We’re hiring at MOIA. If you’re passionate about Go and looking for new opportunities, feel free to get in touch!

A big thank you to everyone who attended the session at GDG Hamburg Meetup.

For further discussions, questions, or just to connect, reach out through Twitter or my website. Let’s keep the conversation going and see how code generation can make our development processes more efficient! :-)

#Go #Talks