golang 自动编译脚本、带版本、格式整理和压缩

开发环境:
centos x86_64 7.3
golang 1.12.3

使用yum安装 upx用于压缩
golang代码main.go部分:

import (
"flag"
"fmt"
"os"
)
var (
VERSION = "unknown"
showVer bool
)
func main() {
flag.BoolVar(&showVer, "V", false, "show version and exit")
flag.Parse()
if showVer {
fmt.Println("version:", _VERSION_)
os.Exit(0)
}
/*主代码*/
}
编译文件Makefile
.PHONY: build clean tool  help
BUILD_VERSION := v0.0.1
programName = "testVersion"
all: build
build:
@go build -v -ldflags "-s -w \
-X main.VERSION=${BUILD_VERSION}" .
bb:
@go build -v -ldflags "-s -w" .
upx -9 $(programName)
tool:
go vet ./…; true
gofmt -w .
clean:
go clean -i .
help:
@echo "make: compile packages and dependencies"
@echo "make tool: run specified go tool"
@echo "make clean: remove object files and cached files"
自动执行autoMake.sh
!/bin/env bash
nPath=pwd
softName=basename $nPath
rm -rf $softName
find . -name ".go" -not -path "./vendor/" -not -path ".git/*" | xargs gofmt -s -d -w
make
upx -9 $softName

执行autoMake.sh,完成编译。

执行 ./testVersion -V 显示版本,不输入直接执行。

发表评论

电子邮件地址不会被公开。 必填项已用*标注