如何配置go语言集成开发环境 vim

Python016

如何配置go语言集成开发环境 vim,第1张

1、编译vimgdb

下载vimgdb73和vim73

mkdir -p ./tmp

cd tmp

tar zxvf ../vim-7.3.tar.gz

unzip ../vimgdb-for-vim7.3-master.zip

mv vimgdb-for-vim7.3-master vimgdb-for-vim7.3

patch -p0 <vimgdb-for-vim7.3/vim73.patch

cd vim73

安装依赖

sudo apt-get install build-essential

sudo apt-get build-dep vim-gtk

sudo apt-get install libncurses5-dev

安装

// 这里直接执行make的操作

make

sudo make install

安装vimgdb runtime

cd ../vimgdb-for-vim7.3

cp vimgdb_runtime ~/.vim/bundle

打开vim

:helptags ~/.vim/bundle/vimgdb_runtime/doc " 生成doc文件

添加配置.vimrc

" vimgdb插件

run macros/gdb_mappings.vim

在vim中执行gdb时,报 “Unable to read from GDB pseudo tty” 的错误,因为没有安装 gdb ,所以安装gdb

sudo apt-get install gdb

2、安装vundle

set up vundle

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

Configure Plugins

在.vimrc文件的开头添加下面的内容,有些不是必须的,可以注掉

set nocompatible " be iMproved, required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/vundle/

call vundle#rc()

" alternatively, pass a path where Vundle should install plugins

"let path = '~/some/path/here'

"call vundle#rc(path)

" let Vundle manage Vundle, required

Plugin 'gmarik/vundle'

" The following are examples of different formats supported.

" Keep Plugin commands between here and filetype plugin indent on.

" scripts on GitHub repos

Plugin 'tpope/vim-fugitive'

Plugin 'Lokaltog/vim-easymotion'

Plugin 'tpope/vim-rails.git'

" 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/'}

" scripts from http://vim-scripts.org/vim/scripts.html

Plugin 'L9'

Plugin 'FuzzyFinder'

" scripts not 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'

" ...

filetype plugin indent on " required

" To ignore plugin indent changes, instead use:

"filetype plugin on

"

" Brief help

" : PluginList - list configured plugins

" : PluginInstall(!)- install (update) plugins

" : PluginSearch(!) foo - search (or refresh cache first) for foo

" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins

"

" see :h vundle for more details or wiki for FAQ

" NOTE: comments after Plugin commands are not allowed.

" Put your stuff after this line

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

3、官方vim-lang插件

Config vim file .vimrc,Add content bellow in bottom of the file

" 官方的插件

" Some Linux distributions set filetype in /etc/vimrc.

" Clear filetype flags before changing runtimepath to force Vim to

" reload them.

filetype off

filetype plugin indent off

set runtimepath+=$GOROOT/misc/vim

filetype plugin indent on

syntax on

autocmd FileType go autocmd BufWritePre Fmt

4、代码补全的插件gocode

配置go的环境变量,比如我的配置,GOPATH变量是必须要配置的,PATH中必须把GOPATH的bin也添加进去,否则没有自动提示,会提示找不到模式

export GOROOT=/usr/local/go

export GOPATH=/data/app/gopath

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Set up gocode

Then you need to get the appropriate version of the gocode, for 6g/8g/5g compiler you can do this:

go get -u github.com/nsf/gocode (-u flag for "update")

Configure vim in .vimrc file

Plugin 'nsf/gocode', {'rtp': 'vim/'}

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

写一个helloword程序,输入fmt后按<C-x><C-o>如果能看到函数的声明展示出来,说明安装是正确的。

4、代码跳转提示godef

Set up godef

go get -v code.google.com/p/rog-go/exp/cmd/godef

go install -v code.google.com/p/rog-go/exp/cmd/godef

git clone https://github.com/dgryski/vim-godef ~/.vim/bundle/vim-godef

Configure vim in .vimrc file

Bundle 'dgryski/vim-godef'

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

5、代码结构提示gotags

Set up gotags

go get -u github.com/jstemmer/gotags

Put the following configuration in your vimrc:

Bundle 'majutsushi/tagbar'

nmap :TagbarToggle

let g:tagbar_type_go = {

\ 'ctagstype' : 'go',

\ 'kinds' : [

\ 'p:package',

\ 'i:imports:1',

\ 'c:constants',

\ 'v:variables',

\ 't:types',

\ 'n:interfaces',

\ 'w:fields',

\ 'e:embedded',

\ 'm:methods',

\ 'r:constructor',

\ 'f:functions'

\ ],

\ 'sro' : '.',

\ 'kind2scope' : {

\ 't' : 'ctype',

\ 'n' : 'ntype'

\ },

\ 'scope2kind' : {

\ 'ctype' : 't',

\ 'ntype' : 'n'

\ },

\ 'ctagsbin' : 'gotags',

\ 'ctagsargs' : '-sort -silent'

\ }

命令模式下按在右边就会显示当前文件下的函数名,结构体名等等,光标放到相应的tag上,按回车可以快速跳到程序中的相应位置。

再次按会关闭tag窗口。

PS:本地的.vimrc的配置

" 插件管理器 vundle

set nocompatible " be iMproved, required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/vundle/

call vundle#rc()

" alternatively, pass a path where Vundle should install plugins

"let path = '~/some/path/here'

"call vundle#rc(path)

" let Vundle manage Vundle, required

Plugin 'gmarik/vundle'

" The following are examples of different formats supported.

" Keep Plugin commands between here and filetype plugin indent on.

" scripts on GitHub repos

" Plugin 'tpope/vim-fugitive'

" Plugin 'Lokaltog/vim-easymotion'

" Plugin 'tpope/vim-rails.git'

" 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/'}

" scripts from http://vim-scripts.org/vim/scripts.html

" Plugin 'L9'

" Plugin 'FuzzyFinder'

" scripts not 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'

" ...

"

filetype plugin indent on " required

" To ignore plugin indent changes, instead use:

" filetype plugin on

"

" Brief help

" : PluginList - list configured plugins

" : PluginInstall(!) - install (update) plugins

" : PluginSearch(!) foo - search (or refresh cache first) for foo

" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins

"

" see :h vundle for more details or wiki for FAQ

" NOTE: comments after Plugin commands are not allowed.

" Put your stuff after this line

syntax on

" ********************************************************************

" 这里省略了其它不相关的插件

" vimgdb插件

run macros/gdb_mappings.vim

" 官方的插件

" Some Linux distributions set filetype in /etc/vimrc.

" Clear filetype flags before changing runtimepath to force Vim to

" reload them.

filetype off

filetype plugin indent off

set runtimepath+=$GOROOT/misc/vim

filetype plugin indent on

syntax on

autocmd FileType go autocmd BufWritePre <buffer>Fmt

" 代码补全的插件

Bundle 'Blackrush/vim-gocode'

" 代码跳转提示

Bundle 'dgryski/vim-godef'

" 代码结构提示

Bundle 'majutsushi/tagbar'

nmap <F8>:TagbarToggle<CR>

let g:tagbar_type_go = {

\ 'ctagstype' : 'go',

\ 'kinds' : [

\ 'p:package',

\ 'i:imports:1',

\ 'c:constants',

\ 'v:variables',

\ 't:types',

\ 'n:interfaces',

\ 'w:fields',

\ 'e:embedded',

\ 'm:methods',

\ 'r:constructor',

\ 'f:functions'

\ ],

\ 'sro' : '.',

\ 'kind2scope' : {

\ 't' : 'ctype',

\ 'n' : 'ntype'

\ },

\ 'scope2kind' : {

\ 'ctype' : 't',

\ 'ntype' : 'n'

\ },

\ 'ctagsbin' : 'gotags',

\ 'ctagsargs' : '-sort -silent'

\ }

1.Docker项目

网址为 https://github.com/docker/docker 。

介绍:Docker是一种操作系统层面的虚拟化技术,可以在操作系统和应用程序之间进行隔离,也可以称之为容器。Docker可以在一台物理服务器上快速运行一个或多个实例。例如,启动一个Cent OS操作系统,并在其内部命令行执行指令后结束,整个过程就像自己在操作系统一样高效。

2.golang项目

网址为 https://github.com/golang/go 。

介绍:Go语言的早期源码使用C语言和汇编语言写成。从Go 1.5版本自举后,完全使用Go语言自身进行编写。Go语言的源码对了解Go语言的底层调度有极大的参考意义,建议希望对Go语言有深入了解的读者读一读。

3.Kubernetes项目

网址为 https://github.com/kubernetes/kubernetes 。

介绍:Google公司开发的构建于Docker之上的容器调度服务,用户可以通过Kubernetes集群进行云端容器集群管理。

4.etcd项目

网址为 https://github.com/coreos/etcd 。

介绍:一款分布式、可靠的KV存储系统,可以快速进行云配置。

5.beego项目

网址为 https://github.com/astaxie/beego 。

介绍:beego是一个类似Python的Tornado框架,采用了RESTFul的设计思路,使用Go语言编写的一个极轻量级、高可伸缩性和高性能的Web应用框架。

6.martini项目

网址为 https://github.com/go-martini/martini 。

介绍:一款快速构建模块化的Web应用的Web框架。

7.codis项目

网址为 https://github.com/Codis Labs/codis。

介绍:国产的优秀分布式Redis解决方案。

8.delve项目

网址为 https://github.com/derekparker/delve 。

介绍:Go语言强大的调试器,被很多集成环境和编辑器整合。