#!/bin/bash # 设置目标文件夹和项目名称 target_dir="./opt/ttk" project_name="ttk" # 创建目标文件夹(若不存在) mkdir -p "$target_dir" # 定义目标平台列表(格式:GOOS/GOARCH) platforms=( "linux/amd64" # Linux 64位 "darwin/amd64" # Mac(Intel芯片)64位 # 如需支持Mac Apple Silicon,可添加:darwin/arm64 ) # 遍历每个平台进行打包 for platform in "${platforms[@]}"; do # 解析平台信息 IFS='/' read -r goos goarch <<< "$platform" # 设置平台相关的环境变量 export CGO_ENABLED=0 # 禁用CGO,确保纯静态编译(跨平台兼容) export GOOS="$goos" export GOARCH="$goarch" # 生成输出文件名(Mac需加.dmg后缀,Linux用可执行文件) if [ "$goos" = "darwin" ]; then output_file="${project_name}-${goos}-${goarch}.dmg" else output_file="${project_name}-${goos}-${goarch}" fi # 执行打包命令 echo "正在打包 $platform 平台..." env GOOS="$goos" GOARCH="$goarch" CGO_ENABLED=0 go build -o "$target_dir/$output_file" . # 检查打包结果 if [ $? -eq 0 ]; then echo "✅ $platform 平台打包成功,文件路径:$target_dir/$output_file" else echo "❌ $platform 平台打包失败" fi done