Hello everyone, my name is Xie Wei, and I am a back-end developer using Go language.
The topic of this article is: A guide to writing Makefiles for Go projects.
1. The premise:
- Use a Makefile
- Write projects using Go
As you write projects, you often need to compile and execute files to see if features are being developed or bugs are being fixed correctly. You can, of course, run the go build command directly to compile, go run
Command to execute.
When writing Go projects, you often perform commands such as testing, format checking, library downloading and installation, etc.
Of course, you can also write shell scripts to execute these commands, further simplifying things.
There is a better option, makefiles. Makefiles are common in many open source projects. When a file changes in your project, you can use the Makefile command to automatically build it
2. The Makefile syntax
PROJECT="example"
default:
echo ${PROJECT}
install:
@govendor sync -v
test: install
@go test. /... .PHONY: default installtest
Copy the code
The above is a very simple Makefile. By writing these commands, you can directly execute make, make install, make test and so on to complete the corresponding commands.
Format introduction:
<target> : <prerequisites>
[tab] <commands>
Copy the code
- Target: a customized command to execute
- Prerequisites: Command executed before target command is executed
- Commands: Commands that are specifically executed
- PHONY PHONY is a built-in keyword
- Without arguments, the first target is executed by default
- @ indicates that echo is disabled, that is, the terminal does not print the actual command
#
Said annotation- ${val} represents a variable, and is declared and used the same way as variables in shell scripts
- Wildcard characters are allowed
3. Go project
Go supports the built-in Go command, which can be used to test, compile, run, and syntax check commands
What commands are often executed in a well-developed Go project?
- Go Vet static examination
- Go Test runs unit tests
- Go FMT formatting
- Go build to compile
- Go run…
So a Makefile for a Go project should also support these commands.
- Make default: compile
- Make FMT: format FMT
- Make a static examination
- Make test: Run a test
- Make install: Download the dependent libraries
- Make clean: Remove compiled binaries
Therefore, the overall arrangement can be as follows:
BINARY="example"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
Copy the code
Added 4.
The Makefile build tool greatly simplifies the difficulty of building projects.
In real production environments, where CI/CD (continuous integration and continuous deployment) is required, Makefiles are also commonly used with CI tools.
For example, for newly merged code, unit tests and static checks are triggered first. After CI scripts are successfully executed, images are constructed and pushed to the server to complete continuous integration and continuous deployment of a whole set of processes.
Makefiles are usually used with Travis.
Such as:
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 - gotest -race ./... -coverprofile=coverage.txt -covermode=atomic
Copy the code
I hope to inspire you.
After < >
Reference:
- Makefile
- TravisCI