Go 多版本管理工具
<h1 id="go-多版本管理工具">Go 多版本管理工具</h1><p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>Go 多版本管理工具<ul><li>一、go get 命令<ul><li>1.1 使用方法:</li></ul></li><li>二、Goenv</li><li>三、GVM (Go Version Manager)</li><li>四、voidint/g<ul><li>4.1 安装</li><li>4.2 冲突</li><li>4.3 使用</li></ul></li></ul></li></ul></div><p></p>
<p>在平时开发中,本地新旧项目并行开发的过程中,你大概率会遇到一个令人头疼的问题,如何同时使用两个不同版本的 Golang Runtime 进行开发呢?</p>
<h2 id="一go-get-命令">一、go get 命令</h2>
<p><strong>这种方法有一个前提,那就是当前系统中已经通过标准方法安装过某个版本的 Go 了。</strong></p>
<h3 id="11-使用方法">1.1 使用方法:</h3>
<p>在项目中初始化 Go Modules:</p>
<pre><code class="language-bash">go mod init <module-name>
</code></pre>
<p>go 版本安装/版本切换,安装不同版本的 Go:</p>
<pre><code class="language-bash">go get golang.org/dl/go<x.y>
go<x.y> download
go<x.y> version
</code></pre>
<p><img src="https://billy.taoxiaoxin.club/md/2023/09/650c2edcc2a1bf1b18a520e5.png" alt="img" loading="lazy"></p>
<p>切换全局 Go 版本:</p>
<pre><code class="language-go">go<x.y> use
</code></pre>
<h2 id="二goenv">二、Goenv</h2>
<p>官网:https://github.com/go-nv/goenv</p>
<p>Goenv 是另一个 Go 多版本管理工具,它的工作原理与其他语言的版本管理工具(如 Ruby 的 RVM 和 Python 的 pyenv)类似。以下是使用 Goenv 的基本步骤:</p>
<p>安装 Goenv(你需要先安装 Git):</p>
<pre><code class="language-bash">git clone https://github.com/syndbg/goenv.git ~/.goenv
</code></pre>
<p>将 Goenv 添加到你的 shell 配置文件(例如 <code>~/.bashrc</code> 或 <code>~/.zshrc</code>)中:</p>
<pre><code class="language-bash">echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.bashrc
echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(goenv init -)"' >> ~/.bashrc
</code></pre>
<p>安装你需要的 Go 版本:</p>
<pre><code class="language-bash">goenv install go1.x.x
</code></pre>
<p>使用特定版本的 Go:</p>
<pre><code class="language-bash">goenv global go1.x.x
</code></pre>
<h2 id="三gvm-go-version-manager">三、GVM (Go Version Manager)</h2>
<p>官网:https://github.com/moovweb/gvm</p>
<p>GVM 是一个流行的 Go 多版本管理工具,它允许你在同一台机器上安装和切换不同版本的 Go。以下是使用 GVM 的基本步骤:</p>
<p>安装 GVM:</p>
<pre><code class="language-go">bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
</code></pre>
<p>安装你需要的 Go 版本:</p>
<pre><code class="language-bash">gvm install go1.x.x
</code></pre>
<p>使用特定版本的 Go:</p>
<pre><code class="language-bash">gvm use go1.x.x
</code></pre>
<h2 id="四voidintg">四、voidint/g</h2>
<h3 id="41-安装">4.1 安装</h3>
<p><code>g</code>是一个 Linux、macOS、Windows 下的命令行工具,可以提供一个便捷的多版本 go 环境的管理和切换。 以下是使用g的基本步骤:</p>
<ul>
<li>
<p>Linux/macOS(适用于 bash、zsh)</p>
<pre><code class="language-bash"># 建议安装前清空`GOROOT`、`GOBIN`等环境变量
curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
echo "unalias g" >> ~/.bashrc # 可选。若其他程序(如'git')使用了'g'作为别名。
source "$HOME/.g/env"
</code></pre>
</li>
<li>
<p>Windows(适用于 pwsh)</p>
<pre><code class="language-bash">iwr https://raw.githubusercontent.com/voidint/g/master/install.ps1 -useb | iex
</code></pre>
</li>
</ul>
<h3 id="42-冲突">4.2 冲突</h3>
<p>这里如果你是 <code>oh-my-zsh</code> 的用户,那么你还需要做一件事,就是解决全局的 <code>g</code>命令的冲突,解决的方式有两种,第一种是在你的 <code>.zshrc</code> 文件末尾添加 <code>unalias</code> :</p>
<pre><code class="language-javascript">echo "unalias g" >> ~/.zshrc # 可选。若其他程序(如'git')使用了'g'作为别名。
# 记得重启 shell ,或者重新 source 配置
</code></pre>
<p>第二种,则是调整 <code>~/.oh-my-zsh/plugins/git/git.plugin.zsh</code> 中关于 <code>g</code> 的注册,将其注释或删除掉:</p>
<pre><code class="language-javascript"># alias g='git'
</code></pre>
<p>我的 <code>.zshrc</code> 中的完整配置:</p>
<pre><code class="language-javascript"># 我的 g 的bin目录调整到了 .gvm ,所以你可能需要一些额外的调整
export PATH="${HOME}/.gvm/bin:$PATH"
export GOROOT="${HOME}/.g/go"
export PATH="${HOME}/.g/go/bin:$PATH"
export G_MIRROR=https://gomirrors.org/
</code></pre>
<h3 id="43-使用">4.3 使用</h3>
<p>查询当前可供安装的<code>stable</code>状态的 go 版本</p>
<pre><code class="language-bash">$ g ls-remote stable
1.19.10
1.20.5
</code></pre>
<p>安装目标 go 版本<code>1.20.5</code></p>
<pre><code class="language-bash">$ g install 1.14.7
Downloading 100% [===============] (92/92 MB, 12 MB/s)
Computing checksum with SHA256
Checksums matched
Now using go1.20.5
</code></pre>
<p>查询已安装的 go 版本</p>
<pre><code class="language-bash">$ g ls
1.19.10
* 1.20.5
</code></pre>
<p>查询可供安装的所有 go 版本</p>
<pre><code class="language-bash">$ g ls-remote
1
1.2.2
1.3
1.3.1
... // 省略若干版本
1.19.10
1.20rc1
1.20rc2
1.20rc3
1.20
1.20.1
1.20.2
1.20.3
1.20.4
* 1.20.5
</code></pre>
<p>切换到另一个已安装的 go 版本</p>
<pre><code class="language-bash">$ g use 1.19.10
go version go1.19.10 darwin/arm64
</code></pre>
<p>卸载一个已安装的 go 版本</p>
<pre><code class="language-bash">$ g uninstall 1.19.10
Uninstalled go1.19.10
</code></pre>
<p>清空 go 安装包文件缓存</p>
<pre><code class="language-bash">$ g clean
Remove go1.18.10.darwin-arm64.tar.gz
Remove go1.19.10.darwin-arm64.tar.gz
Remove go1.20.5.darwin-arm64.tar.gz
</code></pre>
<p>查看 g 版本信息</p>
<pre><code class="language-bash">g version 1.5.0
build: 2023-01-01T21:01:52+08:00
branch: master
commit: cec84a3f4f927adb05018731a6f60063fd2fa216
</code></pre>
<p>更新 g 软件本身</p>
<pre><code class="language-bash">$ g self update
You are up to date! g v1.5.0 is the latest version.
</code></pre>
<p>卸载 g 软件本身</p>
<pre><code class="language-bash">$ g self uninstall
Are you sure you want to uninstall g? (Y/n)
y
Remove /Users/voidint/.g/bin/g
Remove /Users/voidint/.g
</code></pre>
<p>总之,选择其中一个工具并根据你的需求进行设置。这些工具都可以有效地管理不同版本的 Go Runtime,使你能够轻松地在不同项目中切换和使用不同的 Go 版本。</p>
</div>
<div id="MySignature" role="contentinfo">
分享是一种快乐,开心是一种态度!<br><br>
来源:https://www.cnblogs.com/taoxiaoxin/p/17747330.html
頁:
[1]