golang shell cross compiler

#!/usr/bin/env bash
set -e

uname_s=`uname -s | awk '{print tolower($0)}'`
uname_m=`uname -m`
timeTag="-X 'main.BuildTime=$(date '+%Y-%m-%d %H:%M:%S')'"
branchFlag="-X main.GitBranch=$(git name-rev --name-only HEAD)"
commitFlag="-X main.CommitId=$(git rev-parse --short HEAD)"
goVersion=`go version | awk '{print $3}'`
goVersionFlag="-X 'main.GoVersion=${goVersion}'"
staticTag="-extldflags '-static'" #关闭符号链接
ldflags="-s -w ${staticTag} ${timeTag} ${branchFlag} ${commitFlag} ${goVersionFlag} "
CGO_ENABLED=0 GOOS=${uname_s} GOARCH=amd64  go build -ldflags "${ldflags}" -o app main.go

shell string uppercase lowercase turn

uname -s | awk '{print tolower($0)}'  

shell string concatenation

For the connection string or variable, shell provides a very simple approach

Put together directly or can be enclosed in double quotes

 ${staticTag} ${timeTag} ${branchFlag} ${commitFlag} ${goVersionFlag}   

Additional information is compiled with golang

golang we can use at compile time -ldflagsthe compiler option to add some information that is written to the target file

Cross compiler

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

  GOOS: target platform operating system (darwin, FreeBSD, Linux, Windows)
  GOARCH: architecture target platform (386, amd64, arm)
  cross-compiler does not support CGO so to disable it

Guess you like

Origin www.cnblogs.com/tl542475736/p/12073025.html