Easily Manage Go Projects with Makefile: A Development Efficiency Tool

1. Introduction

1. Introduction to make

  • make is a build automation tool that looks for Makefile or makefile files in the current directory.

  • If the corresponding file exists, it will complete the build tasks according to the rules defined within.

2. Introduction to Makefile

  • With Makefile, we no longer need to manually input compilation commands and parameters every time during the compilation process, which greatly simplifies the project compilation process.

  • We can simply understand Makefile as defining the compilation rules for a project file.

  • With Makefile, we no longer need to manually input compilation commands and parameters every time during the compilation process, which greatly simplifies the project compilation process.

  • Using Makefile also allows us to specify the exact compilation rules and processes in the project, and many open-source projects define Makefile files.

3. Installing make on Windows 10

  • MinGW download page: http://sourceforge.net/projects/mingw/files/latest/download?source=files

  • Right-click on Computer -> Properties -> Advanced system settings -> Environment Variables, find PATH in the system variables.

  • Add the address of the bin folder in the MinGW installation directory to PATH.

  • Open the MinGW installation directory, open the bin folder, and rename mingw32-make.exe to make.exe.

  • After these steps, you can input make in the console.

4. Introduction to rules

  • Makefile consists of multiple rules, each rule mainly consists of two parts: the dependency relationship and the execution command.

  • The structure is as follows:

[target] ... : [prerequisites] ...<tab>[command]    ...    ...</tab>
  • Where:

    • targets: the goal of the rule

    • prerequisites: optional files or targets needed to generate the targets.

    • command: the command that make needs to execute (any shell command). There can be multiple commands, each occupying one line.

  • For example:

build:  CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o xx

5. Basic usage of makefile

1. main.go

package main
import (  "fmt"
  "net/http")
func main() {
  http.HandleFunc("/", hello)
  server := &http.Server{
    Addr: ":8888",
  }
  fmt.Println("server startup...")
  if err := server.ListenAndServe(); err != nil {
    fmt.Printf("server startup failed, err:%v\n", err)
  }
}
func hello(w http.ResponseWriter, _ *http.Request) {
  w.Write([]byte("hello v5blog.cn!"))
}

2. Example

  • BINARY="xxx" is defining a variable

  • .PHONY is used to define a phony target that does not create a target file, but executes the commands under this target

.PHONY: all build run gotool clean help
# Name of the compiled project
BINARY="xxx"
# If no parameters are added after make, all is executed by default
all: gotool build

build:  CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY}
run:  @go run ./main.go  #@go run ./main.go conf/config.yaml
gotool:  go fmt ./  go vet ./
clean:  @if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
help:  @echo "make - Format Go code and compile to generate binary files"  @echo "make build - Compile Go code to generate binary files"  @echo "make run - Directly run Go code"  @echo "make clean - Remove binary files and vim swap files"  @echo "make gotool - Run Go tools 'fmt' and 'vet'"

3. Usage

Easily Manage Go Projects with Makefile: A Development Efficiency Tool

6. Complete

.PHONY: all build run gotool clean help
BINARY="bluebell"
all: gotool build
build:  CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o ./bin/${BINARY}
run:  @go run ./main.go conf/config.yaml
gotool:  go fmt ./  go vet ./
clean:  @if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
help:  @echo "make - Format Go code and compile to generate binary files"  @echo "make build - Compile Go code to generate binary files"  @echo "make run - Directly run Go code"  @echo "make clean - Remove binary files and vim swap files"  @echo "make gotool - Run Go tools 'fmt' and 'vet'"

Leave a Comment