在Ubuntu下编写一个简单的Golang编译脚本,你可以使用以下步骤:
首先,确保你已经安装了Go语言。如果没有,请访问Go官方网站下载并安装。
创建一个新的文本文件,例如build.sh
,并在其中输入以下内容:
#!/bin/bash
# 设置GOROOT和GOPATH环境变量
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
# 将Go二进制文件的可执行路径添加到PATH环境变量中
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 切换到包含Go源代码的目录
cd /path/to/your/go/source/code
# 使用go build命令编译Go程序
go build -o your_output_binary_name
# 检查编译是否成功
if [ $? -eq 0 ]; then
echo "Compilation successful!"
else
echo "Compilation failed!"
fi
/path/to/your/go/source/code
:替换为你的Go源代码所在的目录。your_output_binary_name
:替换为你希望生成的可执行文件的名称。chmod +x build.sh
./build.sh
这个脚本将自动设置环境变量,切换到Go源代码目录,并使用go build
命令编译你的程序。如果编译成功,它将输出"Compilation successful!“,否则将输出"Compilation failed!”。