Book Image

Learning Functional Programming in Go

By : Lex Sheehan
Book Image

Learning Functional Programming in Go

By: Lex Sheehan

Overview of this book

Lex Sheehan begins slowly, using easy-to-understand illustrations and working Go code to teach core functional programming (FP) principles such as referential transparency, laziness, recursion, currying, and chaining continuations. This book is a tutorial for programmers looking to learn FP and apply it to write better code. Lex guides readers from basic techniques to advanced topics in a logical, concise, and clear progression. The book is divided into four modules. The first module explains the functional style of programming: pure functional programming, manipulating collections, and using higher-order functions. In the second module, you will learn design patterns that you can use to build FP-style applications. In the next module, you will learn FP techniques that you can use to improve your API signatures, increase performance, and build better cloud-native applications. The last module covers Category Theory, Functors, Monoids, Monads, Type classes and Generics. By the end of the book, you will be adept at building applications the FP way.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Getting the source code


Note

The GitHub repository for this book's source code is https://github.com/l3x/learn-fp-go. If you store your Go projects in the ~/myprojects directory, then run cd ~/myprojects; git clone https://github.com/l3x/learn-fp-go.git. Next, run the cd command into the first project directory: cd ~/myprojects/learn-fp-go/1-functional-fundamentals/ch01-pure-fp/01_oop.

The directory structure of the source files

Directories correspond to the book's units and chapters:

Note

You'll find an executable init script file in all project directories. It's there to make your life easier. When you cd into a project directory, first source the init script. You can do that by either typing source init or . init . (The dot (.) and source commands are interchangeable.) The init script will use Glide to install any dependencies for your project in a vendors directory. For details see the How to build and run Go project section in the Appendix, Miscellaneous Information and How-Tos. PS: Dependencies are third-party Go libraries that our Go application needs to run properly. 

Let's run our first Go application as follows:

Each chapter is divided into sequentially numbered directories that are in the order of their appearance in the book.

How to run our first Go application

First, let's make sure we have Go installed, our GOPATH is properly set, and that we can run a Go application.

Note

If you are using a macOS, then check out the instructions on how to use the brew command to install Go in the appendix; otherwise, to install Go, visit: http://golang.org/doc/install.To set your GOPATH, visit: https://github.com/golang/go/wiki/SettingGOPATH.

Many people use a global GOPATH to store the source code for all their Go applications or, frequently, manually reset their GOPATH. I found this practice to be troublesome when working with multiple Go projects for multiple clients, each of which had differing Go versions and third-party dependencies.

The example Go applications that we'll use in this chapter do not have dependencies; that is, we don't have to import any third-party packages. So, all we have to do to run our first app--cars.go--is verify that Go is installed, set our GOPATH, and type go run cars.go:

Using a global GOPATH is easy for projects that are super simple, like the examples in this chapter.

In Chapter 2, Manipulating Collections, our Go applications will start getting more complex, and we'll get introduced to a simple, more consistent way to manage our Go development environments.