Golang Gin Practice Begins Makefile
Original address:Golang Gin Practice Begins Makefile
Software engineering with a certain complexity basically compiles A, then relies on B, then compiles C … and finally executes the construction.
Wouldn’t it be a collapse if every time people make arrangements, or every new colleague asks you how to build project d, what to pay attention to when rebuilding it, etc?
We often find Makefile in open source projects. Have you ever wondered?
This section will briefly introduce the use of Makefile, and finallyIt is suggested to study deeply.
How to solve
For build choreography, Docker has Dockerfile and in Unix there are artifacts.Make….
What is it?
Make is a build automation tool that looks for Makefile or makefile files in the current directory. If it exists, it will be based on Makefile’sBuild rulesTo complete the construction
Of course, actually within Makefile are specific Shell commands that you write yourself according to the make syntax rules
It is a tool and the rules are simple. Within the scope of support, compiling A, relying on B, and then compiling C is absolutely no problem.
Makefile consists of several rules, each rule starts with a target, followed by a colon, followed by prerequisites for this target
The next line must begin with a tab followed by a command, which is the build command you want this target to execute.
[target] ... : [prerequisites] ... <tab>[command] ... ...
- Target: A target represents a rule and can be one or more file names. It can also be the name (label) of an operation, calledPhony
- Prerequisites: Prerequisites, this item isOptional parameters. It is usually multiple file names and dummy targets. Its function is the criterion of whether target needs to be rebuilt. If the precondition does not exist or has been updated (the last modification time of the file), target needs to be rebuilt.
- Command: Build a specific command set for this target
A simple example
This article willgo-gin-exampleTo write a Makefile, please enter the door of make.
Before writing Makefile, you need to analyze the sequence of construction, dependencies, problems to be solved, etc.
.PHONY: build clean tool lint help all: build build: go build -v . tool: go tool vet . |& grep -v vendor; true gofmt -w . lint: golint ./... clean: rm -rf go-gin-example go clean -i . help: @echo "make: compile packages and dependencies" @echo "make tool: run specified go tool" @echo "make lint: golint ./..." @echo "make clean: remove object files and cached files"
1. In the above documents, the
.PHONYWhich is used to declare build/clean/tool/lint/help asFalse targetWhat would happen if it were declared a false target?
- After being declared as a pseudo target: when executing the corresponding command, make will not check whether there is a file corresponding to build/clean/tool/lint/help, but will run the command corresponding to the label every time.
- If you do not declare that the corresponding file happens to exist, make will think that xx file already exists and there is no need to rebuild it.
2. This piece is relatively simple. You can see the effect when you execute it on the command line and realize the following functions:
- Make: make is make all.
- Make build: compiles packages and dependencies for the current project
- Make tool: runs the specified Go toolset
- Make lint: golint
- Make clean: delete object files and cache files
- make help: help
Why print the executed command
If you have actually done it, you may have doubts. Why is it printed on the standard output when it is only executing commands?
Make prints every command by default before executing it. This behavior is defined asEcho
You can add @, before the corresponding command, to specify that the command will not be printed on the standard output.
build: @go build -v .
So are there any other special symbols? Yes, please find out the use of+,-after class.
This is a relatively concise article, hoping to give you a basic understanding of Makefile.
Congratulations on your necessary skills +1
This series of sample codes
This series of catalogues
- Serial One Golang Introduction and Environmental Installation
- Serialized 2 to build Blog API’s (1)
- Serial 3 to build Blog API’s (2)
- Serial 4 to build Blog API’s (3)
- Serial 5 Use JWT for Identity Verification
- Serial 6 Write a Simple File Log
- Serial Seven Golang Gracefully Restart HTTP Service
- Serial 8 Add Swagger to It
- Serial 9 Deploying Golang Applications to Docker
- Serial Ten Customized GORM Callbacks
- Serial Eleven Cron Scheduled Tasks
- Serial 12 Optimizing Configuration Structure and Realizing Picture Upload
- Serialization 13 Optimize Your Application Structure and Implement Redis Cache
- Serial 14 Realize Export and Import into Excel
- Serial 15 Generate Two-dimensional Code and Merge Posters
- Serial 16 Draw Text on Pictures
- Serial 17 Deploying Go Applications with Nginx
- Cross-compilation of safari Golang
- Please get started with Makefile