Are you tired of seeing Object Oriented code everywhere, with mutations, side-effects and headaches? Luckily, writing Go does not have to be that way! Functional programming is perfectly possible within Go, hence we can leverage FP techniques to make our code more robust, testable and fun.
Functional Programming in Go
Go is a multi-paradigm language, yet most code you encounter âin the wildâ is written in a mostly Object-Oriented way. Yet, Go allows us to write code in a functional way as well, which offers certain advantages over a more traditional âOOâ approach. What follows in this description is also the general flow of how it will be presented.
What is FP?
First weâll start by defining what we mean by functional programming. Superficially Go might not look like what you expect a functional language to look like. But weâre really just missing the syntactic sugar here, as a lot of the underlying concepts that are central to Functional Programming are reflected in Go. Hence it is important to take a look at what many programmers consider requirements for being âfunctionalâ such as: Higher-order functions, recursion (with tail-call optimization), purity, idempotence, .. and how these requirements are met (or arenât met) by Go.
How to leverage them in Go
Once we have convinced ourselves that Go gives us the building blocks for writing FP code, weâll dive into some concrete examples of what we can do with Go. Weâll look at function currying, continuation-passing style programming, pure functions, recursion (without TCO), monads and more. Itâs important to highlight here why we want to use these constructions and when. At best case, youâll learn how to leverage them in your own codebase. At worst, youâll have seen some cool things with Go. Donât be put of if you donât know these terms yet, weâll start with the easy concepts youâve probable already used such as recursion, and work our way up to the complexer ones.
Using libraries
Go actually has libraries that provide an API for programming in a more functional style. Weâll give them an honorable mention but they wonât be the focus of the talk, as you can get started easily without them. But, they do offer certain things we âmissâ in Go by default (like Generics).
Benefits
Writing Go code in this style has numerous benefits over our traditional approach. My goal of this talk is not just to show you cool things you can do with Go, but also why you want to them. Youâll also see introducing them to an existing codebase is easy, and that FP is really not as scary as it might sound!
Downsides
Using this style of programming is not entirely a walk in the park. Thereâs a price to be paid for writing functional code in Go, the main one will be that youâll take a performance hit. But the performance hit might not be where you expect it! Functional programming is one tool in your toolbox, itâll greatly empower you to solve certain problems, whilst itâll help you shoot yourself in the foot for other problems.
Bonus benefits!
Yes, youâll even take away something from this talk you might not have expected! A lot of people think of Haskell when they hear functional programming. Which might have scared them away from functional programming. In this talk youâll get a look at functional programming with a familiar syntax and a language you already love. This will help you understand the underlying concepts and see how they relate to Haskell and other functional languages, where the syntax might be a bit different but the idea remains the same.
Do I need prior knowledge of FP?
No, absolutely not! You donât need to have done functional programming before to benefit from this talk. There are concepts for all levels of understanding of functional programming. If you donât know anything about functional programming yet, youâll discover it in this talk. And if youâre already an FP-wizard who dreams in Haskell, youâll learn how to transfer that understanding to Go.