Member-only story
How to use Makefiles for your Golang development
Hello, in this little post I would like to show you how you can use Makefiles to ease the development of your Golang programs.

“go <command>s” are great by themselves, you can basically manage every part of your Golant development phase with Go’s native tools, so why would I want to use something else? This is a fair question. In many cases I have had the necessity of sharing my codebases or repositories with someone that is not familiar with Golang itself, they won’t know how to build it. Of course you can attack that problem with some documentation in your README.md or some other files specifying how to build your program. A makefile is a great way of abstracting the technical details for someone that just want to try to build our program quickly, in an agnostic way. At the exact moment I see that there is a “Makefile”, I immediately know that there is something I can build with make and optionally with make install.
I don’t remember that long command / I don’t want to type that long command
They are specially useful when you want to reduce your typing or you just want to avoid typos.
Instead of typing this:
go build -o myprogram
./myprogram
I can automate these steps with a custom target such as follows:
Go program (example)
package mainimport "fmt"func main() {
fmt.Println("Hello Make!")
}
Makefile
.DEFAULT_GOAL := buildandrunBIN_FILE=myprogrambuildandrun:
@go build -o "${BIN_FILE}"
./"${BIN_FILE}"
then you just need to run the following command to build and run your program:
$ make
./"myprogram"
Hello Make!
and that is it. Using the “go build …” version requires ~33 characters in my case to build and run the program, I have reduced that to only 4 characters (“make”). This becomes specially useful when you want to abstract multiple things we usually do when we are on a really active development phase, such as: cleaning, running tests, check test coverage, running a linter…