conferences | speakers | series

Functional Programming with Go

home

Functional Programming with Go
FOSDEM 2020

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.

Speakers: Dylan Meeus