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 writing 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 for running unit tests
- go fmt for formatting
- go build for compiling
- go run for running …
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 dependencies
- 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. Supplement
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, newly merged code first triggers unit tests, static checks, etc., then executes CI scripts, and after success, builds the image and pushes it 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)
END
Official site: www.linuxprobe.com
Linux Command Collection: www.linuxcool.com
Teacher Liu Chuan QQ: 5604241
Linux Technology Group: 2261840
(New group, actively joining…)
Readers who want to learn the Linux system can click the “Read the original text” button to learn about the book “Linux Should Be Learned This Way”, which is also very suitable for professional operation and maintenance personnel to read, becoming a high-value tool for assisting your work!