9月 13

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 显示版本,不输入直接执行。

4月 05

centos搭建golang+gin环境

下载解压

wget https://dl.google.com/go/go1.12.1.linux-amd64.tar.gz
tar zxvf go1.12.1.linux-amd64.tar.gz
mv go /usr/local/
cd /usr/local/go
mkdir -p /usr/local/go/workspacego/src/github.com/

vim /etc/profile

配置部分开始

export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export GOARCH=amd64
export GOOS=linux
export GOPATH=$GOROOT/workspacego
export PATH=.:$PATH:$GOBIN

配置部分结束

配置生效

source /etc/profile

下载源码

mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/net.git net
git clone https://github.com/golang/sys.git sys

下载gin

mkdir -p $GOPATH/src/github.com/
cd $GOPATH/src/github.com/
git clone https://github.com/gin-gonic/gin.git