This is a creation in Article, where the information may have evolved or changed.
We need independent workspace.
When we are developing a project, we always want a project to use a single workspace to isolate the dependent environment for each project. In the case of a Ruby project, if the public workspace would cause Gemset to be public, it might be difficult to isolate the different gems of each project at the time of multi-project development, and unexpected problems occurred during development and operation, so the generic Ruby project would be in the project root directory, Use the. Ruby-version and. Ruby-gemset to specify the ruby version of the current project and the Gemset, with the RVM or rbenv and other version tools, so that the Ruby project can use a separate workspace.
So, the Go language project, can you do that?
Project Isolation for Go
In contrast to the Ruby language, the Go language also faces 2 problems in its use:
1.语言版本进步很快,需要对应项目进行语言版本管理。2.第三方库的版本隔离
To solve issue 1, we need a tool to install multiple go versions and provide convenient switching capabilities.
For question 2, we know that go uses the GOPATH environment variable to manage the source code path at compile time, so the third-party library is also downloaded under Gopath, and our source code needs to be gopath. So you need a tool that can be gopath according to the convenient setting.
After searching the internet, I found GVM this handy tool. This tool, developed in Shell scripting, is compatible with bash and zsh, and is similar to Ruby's RVM, enabling the switch of the go language and gopath changes.
Manage workspace with GVM
Installing GVM
GitHub Address: HTTPS://GITHUB.COM/MOOVWEB/GVM
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
If you're using zsh, replace bash with zsh.
Install Go
Here we use Go 1.4.2 version For example, run the following command:
gvm install go1.4.2
Need to use this version, execute gvm use go1.4.2
, if need to make this version as the default version, we can executegvm use go1.4.2 --default
Create a workspace for a project
We are going to create a new project named Baozou. The project directory is placed ~/goproj/
under.
mkdir -p ~/goproj/baozou cd ~/goproj/baozou gvm pkgset create --local gvm pkgset use --local mkdir src
In the above command, we created the directory of the Baozou project, after entering the directory, using the gvm pkgset create -local
command, set the directory to a local pkgset, and then gvm pkgset use --local
use it, the current environment variable Gopath is:
$HOME/goproj/baozou:$HOME/goproj/baozou/.gvm_local/pkgsets/system/local:$HOME/.gvm/pkgsets/system/global
As you can see, Gopath has been set to Baozou this project directory, when we execute the go get
command to download the third-party library, it will be downloaded to the directory by default $HOME/goproj/baozou/src
.
Next, let's create a directory that is really used for code management:
mkdir -p ~/goproj/baozou/src/baozou cd ~/goproj/baozou/src/baozou git init
Here, we use Git to manage the software version of the project. Now, we can ~/goproj/baozou/src/baozou
write code in this directory.
Summarize
By using GVM, we were able to switch the Go language version and switch the Gopath directory based on the project. These 2 features help us to provide independent workspace for each go project, without interfering with each other.