Golang环境及revel框架在Linux下的安装

Golang的安装

1.将下载的存档解压缩到/usr/local中,在/usr/local/go中创建一个Go树

注意:如果先前有安装Go的话,需要在解压缩前删除/usr/local/go,且需要以root用户身份或以sudo命令来运行下面命令

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.8.linux-amd64.tar.gz

2.将/usr/local/go/bin添加到环境变量 PATH 中,可以将以下行添加到$HMOE/.profile 或 /etc/profile中

export PATH=$PATH:/usr/local/go/bin

注意:在下次登录计算机之前,对配置文件所作的更改可能不会用。要立即应用更改,秩序直接运行shell命令或使用如下命令从配置文件执行这些命令即可。

source $HOME/.profile

3.打开终端键入以下命令来验证是否已安装Go

go version

Revel框架的安装

  1. 安装Go
    安装方法同上,以下是补充
    如果尚未创建GOPATH作为安装的一部分,请立即创建。这是一个目录,您的所有GO代码都将在其中。以下是设置它的一种方法:
  • 创建一个目录
mkdir ~/gocode
  • 告诉Go将其用作您的GOPATH
export GOPATH=~/gocode
  • 保存您的GOPATH,以便它将应用于所有环境
echo export GOPATH=~/gocode >> ~/.profile
  1. 安装Git(这里以ubuntu为例)
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git-all

此时在终端中输入如下命令,如果可以看到版本号,证明安装成功

git --version
  1. 安装Revel框架
go get github.com/revel/revel
  1. 安装Revel命令行工具
go get github.com/revel/cmd/revel
  1. 将命令写入到环境变量中
export PATH=$PATH:$GOPATH/bin
  1. 验证是否有效
$ revel
Usage:
  revel [OPTIONS] <command>

Application Options:
  -v, --debug                If set the logger is set to verbose
      --historic-run-mode    If set the runmode is passed a string not json
      --historic-build-mode  If set the code is scanned using the original parsers, not the go.1.11+
  -X, --build-flags=         These flags will be used when building the application. May be specified multiple times, only applicable for Build, Run, Package, Test commands
      --gomod-flags=         These flags will execute go mod commands for each flag, this happens during the build process

Available commands:
  build
  clean
  new
  package
  run
  test
  version

将本机与git建立ssh连接

由于使用 go get 时是通过ssh连接GitHub的,如果没有建立ssh连接,可能会出现连接拒绝的错误。

  1. 配置git与Github关联
git config --global user.name "Your_name"
git config --global user.email "your_email@example.com"
  1. 生成ssh密钥
ssh-keygen -t rsa -C "your_email@example.com"

输入命令之后,一路回车就行

  1. 更新主机密钥
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
  1. 新增ssh密钥到Github账户
  • .ssh/id_rsa.pub 的内容复制到剪贴板上
  • 在网页上打开github,登录账户
  • 在右上角,单击头像,然后单击 Settings
  • 在左侧导航栏,找到 SSH and GPG keys并点击
  • 单击 New SSH key
  • Title 中输入对新密钥添加的描述性标签,在 Key 中将前面复制的内容,粘贴到其中
  1. 测试

在命令行中输入以下命令

ssh git@github.com

在出现的提示中,有出现successfully authenticated,表示授权成功

GO换国内源

因为Go包管理网址使用的是proxy.golang.ory,在国内无法访问,会出现 Get "https://proxy.golang.org/golang.org/x/exp/@v/v0.0.0-20190731235908-ec7cb31e5a56.mod": dial tcp 172.217.160.113:443: i/o timeout 的错误提示

此时,我们需要将go包管理地址换一个国内能访问的代理地址

Go 1.13 及以上(推荐)

打开你的终端并执行

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

完成。

macOS 或 Linux

打开你的终端并执行

export GO111MODULE=on
export GOPROXY=https://goproxy.cn

或者

echo "export GO111MODULE=on" >> ~/.profile
echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
source ~/.profile

完成。

Windows

打开你的 PowerShell 并执行

C:\> $env:GO111MODULE = "on"
C:\> $env:GOPROXY = "https://goproxy.cn"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值