以下是在CentOS上配置Golang打包环境的步骤:
安装Golang
sudo yum install -y golang # CentOS 7
sudo dnf install -y golang # CentOS 8
验证安装:go version
。go1.20.x.linux-amd64.tar.gz
),解压至/usr/local
,并配置环境变量。配置环境变量
编辑~/.bashrc
或/etc/profile
,添加:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
使配置生效:source ~/.bashrc
。
安装依赖工具(可选)
若需编译C依赖的包,安装工具链:
sudo yum install -y gcc gcc-c++ make zlib-devel openssl-devel
项目打包
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o your_app
(CGO_ENABLED=0
用于静态编译,避免依赖动态库)。config.yaml
)打包为压缩包:tar -czvf your_app.tar.gz your_app config/
```。
部署与运行
tar -xzvf your_app.tar.gz
chmod +x your_app
systemd
):/etc/systemd/system/your_app.service
,内容示例:[Unit]
Description=Your Go App
After=network.target
[Service]
ExecStart=/path/to/your_app
Restart=always
User=root
[Install]
WantedBy=multi-user.target
启用并启动服务:systemctl daemon-reload
systemctl start your_app
systemctl enable your_app
```。
说明:
GOOS
和GOARCH
参数(如GOOS=windows
生成.exe
文件)。Go Modules
管理依赖,避免手动处理GOPATH
。