Introduction to fp-go, functional programming for golang by Dr. Carsten Leue

November 7, 2023

Dr. Carsten Leue introduced fp-go, a library he maintains to bring functional programming (FP) principles to Go, an imperative language. Despite Go’s inherent challenges for FP — such as lack of enforced immutability, no type parameters for methods, and invariant generics — Dr. Leue highlighted how fp-go mitigates these issues by providing pure functions, composition, and common FP abstractions like map, filter, and reduce. With over 20 years in software development, Dr. Leue’s journey, influenced by the fp-ts library in TypeScript, emphasizes testability and state management improvements through FP. He demonstrated practical uses of fp-go and addressed performance considerations, noting that while higher-order functions may introduce overhead in micro-scale operations, the overall benefits in maintainability and IO-bound applications prevail. He recommended Functional Programming in Scala for further learning and encouraged experimentation with fp-go, offering support for any queries.

Introduction to fp-go, functional programming for golang: A comprehensive overview

Dr. Carsten Leue began by addressing the question of why functional programming (FP) is beneficial for Go, an imperative language by design. Despite Go’s inherent procedural nature, FP can enhance code testability, manage state effectively, and reduce side effects, leading to more maintainable and robust software. The fp-go library makes it feasible to apply FP principles in Go, opening new possibilities for Go developers.


Dr. Leue’s journey to Functional Programming

Dr. Leue shared his personal journey into FP, influenced heavily by the fp-ts library in TypeScript. With over 20 years of experience in software development, starting from large-scale Java systems to modern cloud services using Go, Python, and TypeScript, he identified the persistent challenges of testability and state management in imperative languages. These challenges motivated him to explore and develop the fp-go library.


Core concepts of Functional Programming

Several core FP concepts are integral to fp-go:

  • Pure functions: Functions that produce the same output given the same input and have no side effects. This predictability makes them highly testable and reusable.
  • Composition: Structuring functions as pure and composing them with standardized functions enhances clarity and modularity.
  • Immutability: Encouraging immutable data structures reduces side effects and makes the code easier to reason about and refactor.
  • Common FP abstractions: Concepts like map, filter, and reduce provide powerful abstractions for data manipulation.


Why Go?

Go was chosen not for its FP capabilities but for its strong ecosystem, including robust crypto libraries and a well-designed concurrency model using goroutines. Go’s compiler toolchain, ease of creating self-contained binaries, and recent support for generics also played a crucial role.


Functional Programming in Go

Dr. Leue highlighted how FP in Go can enhance code correctness through strong typing and explicit function signatures. With the introduction of generics, Go now supports more flexible and type-safe code, allowing a single function to handle multiple types. He also introduced closures, demonstrating how functions can capture values for later use.


Challenges and workarounds

Despite Go’s strengths, it presents challenges for FP:

  • Lack of enforced immutability: The fp-go library ensures immutability, but Go itself does not enforce it.
  • No type parameters for methods: This limitation requires creative workarounds.
  • Invariant generics: Go’s generics are not as flexible as in some other languages, limiting certain FP patterns.
  • Absence of tuples and higher-kinded types: These omissions necessitate additional implementation efforts in the fp-go library.


The fp-go Library

The fp-go library provides a comprehensive set of functions, such as map, filter, reduce, and monads like Either for error handling, Option for optional values, and IO for managing side effects. These tools help reduce boilerplate code and mental complexity, making Go more functional and easier to work with.


Practical examples and performance considerations

Dr. Leue demonstrated practical applications of fp-go, such as using map and reduce functions for data transformation. While acknowledging that higher-order functions can introduce performance overhead in micro-scale operations, he emphasized that the benefits in terms of maintainability and ease of reasoning often outweigh these costs, especially in IO-bound applications.


Conclusion and further learning

Dr. Leue concluded by recommending Functional Programming in Scala by Paul Chiusano and Rúnar Bjarnason as a resource for further learning, noting the similarities between FP in Scala and Go. He encouraged developers to experiment with fp-go and offered his support for any questions or further exploration.


Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.