zoukankan      html  css  js  c++  java
  • Golang-and-package-version-managment

    参考文章

    学习Golang之后对golang中的版本管理,包管理等机制一直没有很好的琢磨,偶然想起还是觉得很有必要进行归纳,包管理使用起来简单,无非就是installuninstalllist等,但是在工程上包管理要是不当,却会造成很多不必要的麻烦。

    gvm

    gvm是golang的版本管理程序,为什么需要这么个版本管理,是因为golang还在高速发展中,发版频繁,升级迭代可能不完全兼容上一代,所以这种工具应运而生,像ruby有对应的rvm,node有对应的nvm都是为了方便的进行不同版本的切换。

    install

    bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
    

    安装完,验证一下是否安装成功

    gvm version
    

    输出

    Go Version Manager v1.0.22 installed at /Users/liujb/.gvm
    

    支持的命令

    % 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
      diff       - view changes to Go root
      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
    

    常见的命令

    1. gvm listall 查看所有的golang版本
    2. gvm install go1.4.2 安装1.4.2版本
    3. gvm use go1.4.2 使用go1.4.2版本,使用后GOPATH变成/Users/liujb/.gvm/pkgsets/go1.4.2/global,这样就相当于每个版本的golang所使用的GOPATH都不一样,这样不同的golang包之间的版本兼容问题也可以得到解决。

    gvp/gpm

    install

    brew install gvp
    brew install gpm
    

    use

    这两个包一般成对使用,其中gvp命令使用主要设置GOPATH为当前目录,例如

    source gvp
    echo $GOPATH    # 输出/Users/liujb/.godeps:/Users/liujb
    

    gpm主要是通过install命令安装Godeps文件内指定的包,最主要的两个命令

    $ gpm get         # Parses the Godeps file, gets dependencies and sets them
                      # to the appropriate version but does not install them.
    $ gpm install     # Parses the Godeps file, installs dependencies and sets
    

    Godeps的文件类似

    github.com/nu7hatch/gotrail               v0.0.2
    github.com/replicon/fast-archiver         v1.02
    launchpad.net/gocheck                     r2013.03.03   # Bazaar repositories are supported
    code.google.com/p/go.example/hello/...    ae081cd1d6cc  # And so are Mercurial ones
    

    通过gpm install时候会先解析Godeps文件,然后安装文件内指定的包以及版本,同时安装到.godeps目录下的src/bin/pkg

    godep

    install

    go get github.com/tools/godep
    或者
    brew install godep
    

    使用

    参考1 参考2

    在项目的根目录下,执行两个命令,其中需要安装的包需要先使用go get下载到$GOPATH下边,另外所在项目需要在版本管理githg 之下。

    godep save
    godep restore
    
    Godeps.json的文件格式如下
    {
        "ImportPath": "github.com/kr/hk",
        "GoVersion": "go1.1.2",
        "Deps": [
            {
                "ImportPath": "code.google.com/p/go-netrc/netrc",
                "Rev": "28676070ab99"
            },
            {
                "ImportPath": "github.com/kr/binarydist",
                "Rev": "3380ade90f8b0dfa3e363fd7d7e941fa857d0d13"
            }
        ]
    }
    

    gom

    install

    go get github.com/mattn/gom
    

    use

    创建Gomfiles

    gom 'github.com/mattn/go-runewidth', :tag => 'go1'
    gom 'github.com/mattn/go-scan', :commit => 'ecb144fb1f2848a24ebfdadf8e64380406d87206'
    gom 'github.com/daviddengcn/go-colortext'
    gom 'github.com/mattn/go-ole', :goos => 'windows'
    
    # Execute only in the "test" environment.
    group :test do
        gom 'github.com/mattn/go-sqlite3'
    end
    
    # Execute only for the "custom_group" group.
    group :custom_group do
        gom 'github.com/golang/lint/golint'
    end
    

    执行

    gom install
    

    然后会在项目下创建_vendor目录。

    % tree -L 4
    ├── Gomfile
    └── _vendor
        └── src
            └── github.com
                ├── daviddengcn
                └── mattn
    
    5 directories, 1 file
    

    个人小结

    1. gvm说白了是golang的版本管理,跟包管理没有半毛钱关系
    2. gvp/gpm这一配套工具,可以设置当前目录为GOPATH,同时根据Godeps文件的描述,安装指定的包到gvp指定的路径。
    3. godep说白了,就是将通过go get安装的包,通过json文件Godeps.json保存到项目下的Godeps/_workspace目录下,然后编译的时候export GOPATH='Project/Godeps/_workspace':$GOPATH即可。
    4. gomnpm最接近,都只是需要一个包描述文件,然后安装到指定位置,其余什么都不用做了,当然别忘了指定GOPATH

    对golang中包的管理机制大概讲这么多,有问题一起沟通。引用注明出处即可。

  • 相关阅读:
    hdu 5072 Coprime (容斥)
    洛谷 P1411 树 (树形dp)
    Tr/ee AtCoder
    sys.path
    uname
    sys.platform
    Eclipse Basic
    Eclipse Color Theme
    Pydev
    scons
  • 原文地址:https://www.cnblogs.com/liujb/p/Golang-and-package-version-managment.html
Copyright © 2011-2022 走看看