Using Makefiles For Go (GoLang) Development

Using Makefiles For Go (GoLang) Development

Learn how to run, build and, compile Go (GoLang) Applications for any Platform and Operating System using a Makefile

1. Introduction

In this short tutorial, we'll cover how to use a Makefile to run, build 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.

build:
	go build -o bin/main main.go

run:
	go run main.go

compile:
	# 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 run, build, and compile Go applications using a Makefile. You can find the source code here on GitHub.

Did you like it? Why don't you try also...

Is String a Palindrome?

Is String a Palindrome?

Learn How to Test if a String is a Palindrome in Go.

How to Convert GoLang Integer to String Conversion

How to Convert GoLang Integer to String Conversion

Learn how to convert integers to strings and how to convert strings to integers in GoLang

Sorting Slices In Go

Sorting Slices In Go

Learn How To Sort Slices in GoLang