Total Pageviews

Saturday, 6 August 2016

go的版本管理器-gvm

go version manager

Build Status
by Josh Bussdieker (jbuss, jaja, jbussdieker)
GVM provides an interface to manage Go versions.

Features

  • Install/Uninstall Go versions with gvm install [tag] where tag is "60.3", "go1", "weekly.2011-11-08", or "tip"
  • List added/removed files in GOROOT with gvm diff
  • Manage GOPATHs with gvm pkgset [create/use/delete] [name]. Use --local as name to manage repository under local path (/path/to/repo/.gvm_local).
  • List latest release tags with gvm listall. Use --all to list weekly as well.
  • Cache a clean copy of the latest Go source for multiple version installs.
  • Link project directories into GOPATH

Background

When we started developing in Go mismatched dependencies and API changes plauged our build process and made it extremely difficult to merge with other peoples changes.
After nuking my entire GOROOT several times and rebuilding I decided to come up with a tool to oversee the process. It eventually evolved into what gvm is today.

Installing

To install: 

curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer | bash
Or if you are using zsh just change bash with zsh

Installing Go

gvm install go1.4
gvm use go1.4 [--default]
Once this is done Go will be in the path and ready to use. $GOROOT and $GOPATH are set automatically.
Additional options can be specified when installing Go:
Usage: gvm install [version] [options]
    -s,  --source=SOURCE      Install Go from specified source.
    -n,  --name=NAME          Override the default name for this version.
    -pb, --with-protobuf      Install Go protocol buffers.
    -b,  --with-build-tools   Install package build tools.
    -B,  --binary             Only install from binary.
         --prefer-binary      Attempt a binary install, falling back to source.
    -h,  --help               Display this message.

A Note on Compiling Go 1.5

Go 1.5+ removed the C compilers from the toolchain and replaced them with one written in Go. Obviously, this creates a bootstrapping problem if you don't already have a working Go install. In order to compile Go 1.5+, make sure Go 1.4 is installed first.
gvm install go1.4 -B
gvm use go1.4
export GOROOT_BOOTSTRAP=$GOROOT
gvm install go1.5

List Go Versions

To list all installed Go versions (The current version is prefixed with "=>"):
gvm list
To list all Go versions available for download:
gvm listall

Uninstalling

To completely remove gvm and all installed Go versions and packages:
gvm implode
If that doesn't work see the troubleshooting steps at the bottom of this page.

Mac OS X Requirements

xcode-select --install
brew update
brew install mercurial

Linux Requirements

Debian/Ubuntu

sudo apt-get install curl git mercurial make binutils bison gcc build-essential

Redhat/Centos

sudo yum install curl
sudo yum install git
sudo yum install make
sudo yum install bison
sudo yum install gcc
sudo yum install glibc-devel

FreeBSD Requirements

sudo pkg_add -r bash
sudo pkg_add -r git
sudo pkg_add -r mercurial

Vendoring Native Code and Dependencies

GVM supports vendoring package set-specific native code and related dependencies, which is useful if you need to qualify a new configuration or version of one of these dependencies against a last-known-good version in an isolated manner. Such behavior is critical to maintaining good release engineering and production environment hygiene.
As a convenience matter, GVM will furnish the following environment variables to aid in this manner if you want to decouple your work from what the operating system provides:
  1. ${GVM_OVERLAY_PREFIX} functions in a manner akin to a root directory hierarchy suitable for auto{conf,make,tools} where it could be passed in to ./configure --prefix=${GVM_OVERLAY_PREFIX} and not conflict with any existing operating system artifacts and hermetically be used by your workspace. This is suitable to use with C{PP,XX}FLAGS and LDFLAGS, but you will have to manage these yourself, since each tool that uses them is different.
  2. ${PATH} includes ${GVM_OVERLAY_PREFIX}/bin so that any tools you manually install will reside there, available for you.
  3. ${LD_LIBRARY_PATH} includes ${GVM_OVERLAY_PREFIX}/lib so that any runtime library searching can be fulfilled there on FreeBSD and Linux.
  4. ${DYLD_LIBRARY_PATH} includes ${GVM_OVERLAY_PREFIX}/lib so that any runtime library searching can be fulfilled there on Mac OS X.
  5. ${PKG_CONFIG_PATH} includes ${GVM_OVERLAY_PREFIX}/lib/pkgconfig so that pkg-config can automatically resolve any vendored dependencies.
Recipe for success:
gvm use go1.1
gvm pkgset use current-known-good
# Let's assume that this includes some C headers and native libraries, which
# Go's CGO facility wraps for us.  Let's assume that these native
# dependencies are at version V.
gvm pkgset create trial-next-version
# Let's assume that V+1 has come along and you want to safely trial it in
# your workspace.
gvm pkgset use trial-next-version
# Do your work here replicating current-known-good from above, but install
# V+1 into ${GVM_OVERLAY_PREFIX}.
See examples/native for a working example.

Troubleshooting

Sometimes especially during upgrades the state of gvm's files can get mixed up. This is mostly true for upgrade from older version than 0.0.8. Changes are slowing down and a LTR is imminent. But for now rm -rf ~/.gvm will always remove gvm. Stay tuned!

from  https://github.com/moovweb/gvm

参考:
https://www.moovweb.com/gvm-the-go-version-manager/
https://gocn.io/article/107
http://chen-tao.github.io/2017/09/14/Use-gvm-manage-golang-version/
------------
ruby有rvm(或rbenv),nodejs有nvm(https://github.com/creationix/nvm),现在,go则有了gvm.
------------
还有其他方案:https://github.com/syndbg/goenv(这个更简单)
https://github.com/kaneshin/goenv
--------------

树莓派通过GVM管理多版本的Golang

关于gvm

GVM,类似于ruby 中的RVM,java 中的jenv(国产),可用于方便管理 Go 的版本,它有如下几个特性:
  • 管理 Go 的多个版本,包括安装、卸载和指定使用 Go 的某个版本
  • 查看官方所有可用的 Go 版本,同时可以查看本地已安装和默认使用的 Go 版本
  • 管理多个 GOPATH,并可编辑 Go 的环境变量
  • 可将当前目录关联到 GOPATH
  • 可以查看 GOROOT 下的文件差异

Go 版本切换

二进制文件的管理比较简单,通过链接使用不同版本的程序即可,实际上主要是一些环境变量和标准库的设置问题,环境变量主要是 GOPATH 以及 GOROOT,标准库的话需要在切换 go 版本时也能跟着切换。gvm 实际上就是帮助完成这些配置工作。

安装

按照官方文档操作即可

1
2
3
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
# or
zsh < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

使用


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ gvm
Usage: gvm [command]
Description:
GVM is the Go Version Manager
Commands:
version - print the gvm version number
get - gets the latest code (for debugging)
use - select a go version to use (--default to set permanently)
diff - view changes to Go root
help - display this usage text
implode - completely remove gvm
install - install go versions
uninstall - uninstall go versions
cross - install go cross compilers
linkthis - link this directory into GOPATH
list - list installed go versions
listall - list available versions
alias - manage go version aliases
pkgset - manage go packages sets
pkgenv - edit the environment for a package set
Mac下需要这样一把(如果你的环境有问题的话)

1
2
3
xcode-select --install
brew update
brew install mercurial
一般常规的操作是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 先列一下看支持哪些版本
gvm listall
# 看下自己环境有哪些版本
gvm list
# 安装需要的版本,比如go1.8.3,加--default代表每次再进terminal时默认使用这个版本
gvm install go1.8.3 [--default]
# 然后看下是否成功
go version
go env
# 如果不需要gvm的环境变量,则需要到GVM_ROOT/environments下面去删除
# 注意是清空文件
> $GVM_ROOT/environments/go1.8.3
# 如果需要每次打开terminal都生效的话,以下命令加入到bashrc/zshrc,注意要加在GOPATH/GOROOT的setting之前(如果希望环境变量不被修改的话)
source /Users/chentao/.gvm/scripts/gvm

几点注意

  • 在 Linux 下安装 GVM,需要安装相关依赖,详情见官网说明,或者执行以下命令
  • ``` Debian/Ubuntu
sudo apt-get install curl git mercurial make binutils bison gcc build-essential
Redhat/Centos
sudo yum install curl sudo yum install git sudo yum install make sudo yum install bison sudo yum install gcc sudo yum install glibc-devel
FreeBSD Requirements
sudo pkg_add -r bash sudo pkg_add -r git sudo pkg_add -r mercurial ```
  • GVM 官方显示没对 Windows 做支持,考虑到 Windows 不是程序员的标配吧(有同学回复用 git-windows客户端,自带bash功能,可以自行尝试安装下)。
  • GVM 安装新版本之后,Go 环境变量会重新设置,为了节约磁盘空间和时间,建议把之前版本的 GOPATH 内容移动到新版本对应的 GOPATH 目录下,相关路径可以通过 go env 获取
  • 在同一版本下,用 gvm pkgset 创建多个 GOPATH,可能会占用你比较大的磁盘空间
  • 使用 gvm implode 需谨慎
  • GVM 是用 shell 编写的。