Vim go语言开发环境配置

安装vim 和 vim-go 

sudo apt-get install vim
git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go

安装Vundle

sudo apt-get install git
sudo apt-get install curl

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

编辑配置文件

vim ~/.vimrc

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()
"
" " let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
"https://github.com/fatih/vim-go
Plugin 'https://github.com/Valloric/YouCompleteMe'

" " All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

set number
set autoindent
set shiftwidth=4
set ts=4
set expandtab
set softtabstop=4
set paste

set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set termencoding=utf-8
set fileformats=unix
set encoding=utf-8

保存后再次打开vim,输入 PluginInstall ,会出现一个子窗口,等待下面出现Done!

cd ~/.vim/bundle ,这时候可以进入 Vundle 目录看到出现了 vim-go 目录。

安装Go插件项目

vim ~/.vim/bundle/vim-go/plugin/go.vim

配置文件里面的 s:packages 要注意,由于某种原因无法访问某些,所以从 https://www.golangtc.com/download/package 下载后到 GOPATH/src 里,然后cd进入项目目录,进行 go build 生成可执行文件再放在GOPATH/bin 下。

一般来说package main 的都在项目根目录,也有特例比如说 golint 要到 目录下的 golint 进行编译才能生成可执行文件。

打开 vim 输入 GoInstallBinaries 就开始安装了,注意看提示信息,缺哪个就编译哪个放进去。

再次编辑任意的Go文件:

- 新起一行输入fmt.,然后ctrl+x, ctrl+o,Vim 会弹出补齐提示下拉框,不过并非实时跟随的那种补齐,这个补齐是由gocode提供的。
– 输入一行代码:time.Sleep(time.Second),执行:GoImports,Vim会自动导入time包。
– 将光标移到Sleep函数上,执行:GoDef或命令模式下敲入gd,Vim会打开$GOROOT/src/time/sleep.go中 的Sleep函数的定义。执行:b 1返回到hellogolang.go。
– 执行:GoLint,运行golint在当前Go源文件上。
– 执行:GoDoc,打开当前光标对应符号的Go文档。
– 执行:GoVet,在当前目录下运行go vet在当前Go源文件上。
– 执行:GoRun,编译运行当前main package。
– 执行:GoBuild,编译当前包,这取决于你的源文件,GoBuild不产生结果文件。
– 执行:GoInstall,安装当前包。
– 执行:GoTest,测试你当前路径下地_test.go文件。
– 执行:GoCoverage,创建一个测试覆盖结果文件,并打开浏览器展示当前包的情况。
– 执行:GoErrCheck,检查当前包种可能的未捕获的errors。
– 执行:GoFiles,显示当前包对应的源文件列表。
– 执行:GoDeps,显示当前包的依赖包列表。
– 执行:GoImplements,显示当前类型实现的interface列表。
– 执行:GoRename [to],将当前光标下的符号替换为[to]。

安装实时代码提示

上面配置文件中已经加上了  Plugin 'https://github.com/Valloric/YouCompleteMe'  ,如果新输入的话保存退出,再打开Vim输入PluginInstall。

apt-get install build-essential cmake python-dev

cd ~/.vim/bundle/ 
git clone https://github.com/Valloric/YouCompleteMe.git
cd YouCompleteMe/
git submodule update --init --recursive

cd ~/.vim/bundle/YouCompleteMe
/install.py --gocode-completer

猜你喜欢

转载自blog.csdn.net/hjmnasdkl/article/details/84584551