一个脚本打包三个平台的linux,mac,windows的golang程序:
#!/bin/bash
# 设置变量
APP_NAME="atmp"
VERSION="1.0.0"
BUILD_TIME=$(date +%Y-%m-%d_%H:%M:%S)
BUILD_DIR="bin"
PLATFORMS=("linux/amd64" "windows/amd64" "darwin/amd64")
# 创建输出目录
mkdir -p $BUILD_DIR
# 循环打包不同平台
for PLATFORM in "${PLATFORMS[@]}"
do
# 解析平台参数
OS=$(echo $PLATFORM | cut -d'/' -f1)
ARCH=$(echo $PLATFORM | cut -d'/' -f2)
# 设置输出文件名
OUTPUT="$BUILD_DIR/$APP_NAME-$VERSION-$OS-$ARCH"
# 打包程序
if [ "$OS" == "windows" ]; then
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -o $OUTPUT.exe -ldflags "-X main.version=$VERSION -X main.buildTime=$BUILD_TIME" main.go
elif [ "$OS" == "linux" ]; then
CGO_ENABLED=1 GOOS=$OS GOARCH=$ARCH CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ go build -o $OUTPUT -ldflags "-X main.version=$VERSION -X main.buildTime=$BUILD_TIME" main.go
else
go build -o $OUTPUT -ldflags "-X main.version=$VERSION -X main.buildTime=$BUILD_TIME" main.go
fi
done
标签:windows,golang,mac,VERSION,BUILD,linux,go,main,OS
From: https://blog.51cto.com/thinklili/9219046