Level Up Your Go Game: Comprehensive guide to Function Types and Flexible Programming

Kirubakaran
4 min readJun 14, 2024

Imagine, You’re at a busy Indian wedding, and the caterers are running around like anything. They have different tasks: one guy is making dosas, another is mixing badam milk, and yet another is grilling paneer.

Now, imagine you’re the head chef. You don’t want to micromanage every single task, right? You just want to tell each person what they need to do, not how to do it. That’s where “function types” come in.

Think of function types as job descriptions for your catering crew. For example, the dosa-maker’s job description might be “takes batter, gives back dosa.” The lassi-mixer’s might be “takes yogurt and fruits, gives back lassi.”

Now, you, the head chef, can write a general instruction like “take this ingredient, and make the dish.” This instruction can be used for any chef, as long as their job description matches the ingredient. For example, if you give batter to the dosa-maker, they know what to do. If you give yogurt and fruits to the lassi-mixer, they’ll whip up a delicious drink.

This is how function types work in Go. They’re like job descriptions that tell you what a function does. You can write general code that works with any function, as long as the function type matches. This makes your code more flexible and…

--

--

Kirubakaran
Kirubakaran

Written by Kirubakaran

Software Engineer expertise on C, C++, Golang, Python, Docker, Kubernetes.