2014-11-09 wcdj

摘要:之前总结过在subl中使用GoLang,《GoLang及Sublime Text 2之Mac OS X 10.8.4开发环境安装 》。其实GoLang的安装包中已经包含了支持Vim的编写插件,配置方法很简单,可参考《GoLang之Gvim/Vim配置》。本文再讨论下使用Vundle如何在Vim中配置GoLang开发环境vim-go


$ git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim


" -------------
" Vundle
" https://github.com/gmarik/Vundle.vim
" -------------

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
""Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
""Plugin 'L9'
" Git plugin not hosted on GitHub
""Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
""Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
""Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
""Plugin 'user/L9', {'name': 'newL9'}

" Install Vim-go
Plugin 'fatih/vim-go'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

其中,配置中的 Plugin 'fatih/vim-go' 告诉Vundle我们想要安装vim-go这个插件,安装方法如下:

先用vim打开任意一个go源文件(假如之前并未配置过GoLang开发环境,确保~/.vim/syntax下没有使用vim.go,打开go的源文件后不会有对应的语法显示),例如,hello.go。然后使用命令 :PluginInstall 就可以安装vim-go了,安装成功后会在最下面显示Done的字样。


接下来的工作:(install necessary Go tools)

Please be sure all necessary binaries are installed (such as gocode,godef,goimports, etc..). You can easily install them with the included:GoInstallBinaries. Those binaries will be automatically downloaded andinstalled to your$GOBIN environment (if not set it will use $GOPATH/bin).It requiresgit and hg for fetching the individual Go packages.

在Vim中使用命令 :GoInstallBinaries 会使用hg下载vim-go使用的二进制工具,具体源码可以查看文件:~/.vim/bundle/vim-go/plugin/go.vim

" these packages are used by vim-go and can be automatically installed if
" needed by the user with GoInstallBinaries
let s:packages = [
            \ "github.com/nsf/gocode", 
            \ "code.google.com/p/go.tools/cmd/goimports", 
            \ "code.google.com/p/rog-go/exp/cmd/godef", 
            \ "code.google.com/p/go.tools/cmd/oracle", 
            \ "code.google.com/p/go.tools/cmd/gorename",
            \ "github.com/golang/lint/golint", 
            \ "github.com/kisielk/errcheck",
            \ "github.com/jstemmer/gotags",
            \ ]

或者使用go get进行下载:

gerryyang@mba:~$go get github.com/kisielk/errcheck
package code.google.com/p/go.tools/go/loader: Get https://code.google.com/p/go/source/checkout?repo=tools: dial tcp operation timed out
package code.google.com/p/go.tools/go/types: Get https://code.google.com/p/go/source/checkout?repo=tools: dial tcp operation timed out

如果下载失败,也可以通过 gopm.io (Download Go packages with version, but no require for version control tools like Git and Hg, etc.) 根据路径单独下载。


[1] Golang开发环境搭建-Vim篇

[2] Go development environment for Vim