Before you go, check out these stories!

Hackernoon logoHow To Use Makefiles for Go (Golang) Development by@harrisonbrock

How To Use Makefiles for Go (Golang) Development

Author profile picture

@harrisonbrockHarrison Brock

1. Introduction

In this short tutorial, we’ll cover how to use a Makefile to runbuild and, compile your applications to any Operating System and Platform supported by Go (GoLang).

2. Simple Go Application

First, we will create a basic Go (GoLang) Application:

package main

import "fmt"

func main() {
	fmt.Println("Hello World!!")

3. Create Makefile

Makefiles are an automation tool for build and running software application. Below is a Makefile example that will build, run, and compile our Go application.

	go build -o bin/main main.go

	go run main.go

	# 32-Bit Systems
	# FreeBDS
	GOOS=freebsd GOARCH=386 go build -o bin/main-freebsd-386 main.go
	# MacOS
	GOOS=darwin GOARCH=386 go build -o bin/main-darwin-386 main.go
	# Linux
	GOOS=linux GOARCH=386 go build -o bin/main-linux-386 main.go
	# Windows
	GOOS=windows GOARCH=386 go build -o bin/main-windows-386 main.go
        # 64-Bit
	# FreeBDS
	GOOS=freebsd GOARCH=amd64 go build -o bin/main-freebsd-amd64 main.go
	# MacOS
	GOOS=darwin GOARCH=amd64 go build -o bin/main-darwin-amd64 main.go
	# Linux
	GOOS=linux GOARCH=amd64 go build -o bin/main-linux-amd64 main.go
	# Windows
	GOOS=windows GOARCH=amd64 go build -o bin/main-windows-amd64 main.go

4. Run Makefile Commands

Time to go over the 3 Makefile commands.

4.1 Build Command

To build our Go (GoLang) application for our platform we run this command:

make build

4.2 Run Command

To run our Go (GoLang) application we run this command:

make run

4.3 Compile Command

To compile our Go (GoLang) application for different platforms and operating systems we run this command:

make compile

5. Conclusion

In this short tutorial, you learned how to runbuild, and compile Go applications using a Makefile. You can find the source code here on GitHub.


The Noonification banner

Subscribe to get your daily round-up of top tech stories!