在Linux中如何使用GO作为脚本语言
一、安装GO环境
(注意:得使用root用户进行安装操作,线上操作需注意)
1.1 下载
wget https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz
如果由于网络问题或者权限问题导致线上或其他情况不能直接wget下载压缩包,则可以先手动下载到自己电脑上,然后再传一下。
1.2 解压安装
执行tar解压到/usr/loacl目录下,得到go文件夹
tar -C /usr/local -zxvf go1.10.3.linux-amd64.tar.gz
1.3 添加环境变量
添加/usr/loacl/go/bin目录到PATH变量中。添加到/etc/profile 或$HOME/.profile都可以
// 习惯用vim,没有的话可以用命令`sudo apt-get install vim`安装一个
vim /etc/profile
// 在最后一行添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
// wq保存退出后source一下
source /etc/profile
1.4 确认
执行go version,如果现实版本号,则Go环境安装成功。
Linux下安装GO环境 https://studygolang.com/articles/13957?fr=sidebar
二、编写简单脚本进行测试
参考如下:
在Linux中如何使用GO作为脚本语言 https://studygolang.com/articles/12461?fr=sidebar
2.1 线上环境问题
问题:在线上环境中无法安装github上的一些第三方库,,,
原因:为了安全,只有中控机能出外网
解决方案:使用能访问外网的测试机重新搭建测试环境
2.2 执行环境问题
注意:
在安装gorun的时候,应该
mv ~/go/bin/gorun /usr/bin/
而不是
mv ~/go/bin/gorun /usr/local/bin/
否则会出现:
bash: gorun: command not found