Makefile Guide for Go Projects

This article is a Makefile guide for Go projects.

Hello everyone, my name is Xie Wei, and I am a backend developer using the Go language.

The theme of this article is: Writing a Makefile guide suitable for Go projects.

1. Prerequisites:

  • Familiarity with Makefile
  • Experience in writing projects in Go

During the project development process, it is often necessary to compile and execute files to check whether the functionality developed or bugs fixed are correct. You can certainly execute the go build command to compile and use the go run command to execute.

In fact, while developing Go projects, you will often execute commands such as testing, format checking, and library installation.

Of course, you can also write shell scripts to execute these commands for further simplification.

However, there is a better option, which is Makefile. You can often see Makefile in many open-source projects. When files in your project change, you can use Makefile to execute commands for automatic builds.

2. Makefile Syntax

PROJECT="example"

default:
    echo ${PROJECT}

install:
    @govendor sync -v

test: install
    @go test ./...

.PHONY: default install test


The above is a very simple Makefile. By writing these commands, you can directly execute make, make install, make test, etc., to complete the corresponding commands.

Format introduction:

<target> : <prerequisites> 
[tab]  <commands> 

  • target: The custom command you want to execute
  • prerequisites: The prerequisites, i.e., commands to be executed before the target command
  • commands: The specific commands to be executed
  • .PHONY is a built-in keyword for phony targets
  • Without parameters, the first target is executed by default
  • @ indicates to suppress echo, meaning the terminal will not print the actual executed command
  • # indicates a comment
  • ${val} indicates a variable, consistent with variable declaration and usage in shell scripts
  • Wildcard usage is allowed

3. Go Projects

Go supports built-in go commands that can be used for: testing, compiling, running, syntax checking, etc.

What commands are commonly executed in a complete Go project?

  • go vet for static analysis
  • go test to run unit tests
  • go fmt for formatting
  • go build for compilation
  • go run for execution …

Therefore, a Makefile suitable for Go projects should also support these commands.

  • make default: Compile
  • make fmt: Format
  • make vet: Static analysis
  • make test: Run tests
  • make install: Download dependency libraries
  • make clean: Remove compiled binary files

So the overall arrangement can be as follows:

BINARY="example"
VERSION=1.0.0
BUILD=`date +%FT%T%z`

PACKAGES=`go list ./... | grep -v /vendor/`
VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/`
GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"`

default:
    @go build -o ${BINARY} -tags=jsoniter

list:
    @echo ${PACKAGES}
    @echo ${VETPACKAGES}
    @echo ${GOFILES}

fmt:
    @gofmt -s -w ${GOFILES}

fmt-check:
    @diff=?(gofmt -s -d $(GOFILES)); \
    if [ -n "$${diff}" ]; then \
        echo "Please run 'make fmt' and commit the result:"; \
        echo "$${diff}"; \
        exit 1; \
    fi;

install:
    @govendor sync -v

test:
    @go test -cpu=1,2,4 -v -tags integration ./...

vet:
    @go vet $(VETPACKAGES)

docker:
    @docker build -t wuxiaoxiaoshen/example:latest .

clean:
    @if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: default fmt fmt-check install test vet docker clean


4. Additional Information

The Makefile build tool greatly simplifies the difficulty of building projects.

In a real production environment, CI/CD (Continuous Integration and Continuous Deployment) is often used, so Makefile is also commonly used in conjunction with CI tools.

For example, when new code is merged, unit tests and static checks are triggered first, then CI scripts are executed, and after success, the image is built and pushed to the server, completing the entire process of continuous integration and continuous deployment.

Makefile is often used with Travis.

For example:

language: go
go:
  - "1.11"
  - "1.11.x"
env:
  - GO111MODULE=on
notifications:
  email:
    recipients:
      - [email protected]
    on_success: change # default: change
    on_failure: always # default: always

before_install:
  - go test -cpu=1,2,4 -v -tags integration ./...
  - go vet $(go list ./... | grep -v /vendor/)

script:
  - make fmt
  - make fmt-check
  - make vet
  - make list  
  - go test -race  ./... -coverprofile=coverage.txt -covermode=atomic

I hope this provides some inspiration for everyone.

Source:https://juejin.cn/post/6844903806971412494

Article reproduced from: Go Development Collection

(Copyright belongs to the original author, please delete if infringed)

Makefile Guide for Go Projects

Makefile Guide for Go Projects

Makefile Guide for Go Projects

Click the “Read the original text” below for more.

Leave a Comment