We welcome you to subscribe, read, comment and like!!!!

preface

Laziness can sometimes promote progress. We developed all kinds of automated scripts and stuff because we were lazy. We study the program and let the computer do the work for us. So, in the Go world, is there a way to automate writing tasks for our applications?

Today, I have one for you, which is the MakeFile.

When you are often involved in Golang large projects, it often takes time to keep building and testing the commands go build XX, go test XX repeatedly.

Makefiles provide a very efficient way to automate writing tasks for our application.

Learn how to easily set up your workflow using makefiles!

Note: Although the make toolchain is very popular, it is limited to UNIX platforms. Therefore, if you are using Windows, you may need to install a Linux environment (such as Msys/WSL) inside Windows to use the make command.

Use makefiles in Go

The main functional use case of the build file is to use “tags” to easily run different tasks. Using tags, make TAGNAME will run only the corresponding task TAGNAME.

So, let’s assume an example program main.go. To build this file, you usually need to run the following command:

go build main.go
Copy the code

To execute the program directly, do the following:

go build -o main.out main.go
./main.out
Copy the code

However, with a makefile, you can create two tasks build, run instead.

BINARY_NAME=main.out
 
build:
    go build -o ${BINARY_NAME} main.go
 
run:
    go build -o ${BINARY_NAME} main.go
    ./${BINARY_NAME}
 
clean:
    go clean
    rm ${BINARY_NAME}
Copy the code

Now you can run the Build and Run tasks as follows:

make build
make run
Copy the code

If you want to add a default tag to invoke the necessary tasks, we can use the all tag:

BINARY_NAME=main.out
 
all: build test
 
build:
    go build -o ${BINARY_NAME} main.go
 
test:
    go test -v main.go
 
run:
    go build -o ${BINARY_NAME} main.go
    ./${BINARY_NAME}
 
clean:
    go clean
    rm ${BINARY_NAME}
Copy the code

Now main.go, this will build and test our program when we run it:

make
Copy the code

See how easy our job has become? That’s why these build chains are so useful!

Note that you can use shell variables in makefiles. The variable BINARY_NAME is actually main.out, so the dollar sign will perform the variable substitution. You can run as many complex tasks as you need.

For example, if your project has multiple dependencies that need to be installed, go Get package-name can also be automated!

We can create a separate task called DEPS that will install all the relevant packages directly.

For example, if my project requires the Gorilla Websocket library, I can perform the following tasks:

deps:
    go get github.com/gorilla/websocket
Copy the code

Runtime:

make deps
Copy the code

All dependencies are installed.

conclusion

Hopefully today you’ll learn how to automate build tasks and quickly set up the Golang project workflow.

Walk up, our learning steps can not stop!