Vim打造成GoLang开发环境

时间:2021-05-25

源码安装GoLang
进入:https://golang.google.cn/dl/

下载:wget https://golang.google.cn/dl/go1.14.6.darwin-amd64.tar.gz

安装: tar -xzf go1.14.6.linux-amd64.tar.gz -C /usr/local

配置环境变量:vim ~/.bash_profile

export GOROOT=/usr/local/go
export GOPATH=/Users/why/Desktop/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
 
alias gosrc="cd $GOROOT/src"
alias gopath="cd $GOPATH"
alias gomod="cd $GOMOD"
 
alias gorm='gormt -g=true'
alias gotest='gotests -all -w ./'
alias goformat='gofmt -l -w -s ./'
alias goimport='goimports -l -w ./'
alias gorace='go run -race .'
配置go env:

go env -w GO111MODULE=on
go env -w GOPROXY="https://goproxy.io,direct"
go env -w GOROOT=/usr/local/go
go env -w GOBIN=/usr/local/go/bin
go env -w GOPATH=/Users/why/go
安装Go相关工具
这里只列出来一部分,剩下的按需安装

go get -u -v github.com/mdempsky/gocode
go get -u -v golang.org/x/tools/gopls
go get -u -v github.com/uudashr/gopkgs/v2/cmd/gopkgs
go get -u -v github.com/ramya-rao-a/go-outline
go get -u -v github.com/acroca/go-symbols
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v github.com/go-delve/delve/cmd/dlv
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/sqs/goreturns
go get -u -v golang.org/x/lint/golint
go get -u -v github.com/cweill/gotests/...
go get -u -v github.com/fatih/gomodifytags
go get -u -v github.com/josharian/impl
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -u -v github.com/haya14busa/goplay/cmd/goplay
go get -u -v github.com/godoctor/godoctor
go get -u -v github.com/smartystreets/goconvey  
go get -u -v github.com/jstemmer/gotags
go get -u -v golang.org/x/tools/cmd/goimports 
go get -u -v  golang.org/x/tools/cmd/godoc
go get -u -v github.com/golangci/golangci-lint/cmd/golangci-lint
go get -u -v github.com/xxjwxc/gormt@master (gormt -g=true)
安装代码规范检测工具
这个弥补 golint 的不足

brew install golangci-lint
Vim编译器配置
vim-go 必须保证 vim 版本是8以上,运行 vim 命令查看,可以通过源码安装,如果已经是是请忽略。

yum remove vim
github下载源码:https://github.com/vim/vim
cd ./vim/src
./configure
make && make install
提前下载 vim 插件源码包:

mkdir ~/.vim/bundle  
cd ~/.vim/bundle 
 
git clone https://github.com/VundleVim/Vundle.vim.git
git clone https://github.com/fatih/vim-go.git
git clone https://github.com/dgryski/vim-godef
git clone https://github.com/Blackrush/vim-gocode.git
git clone https://github.com/mattn/vim-goimports.git
git clone https://github.com/jstemmer/gotags.git
 
mkdir ~/.vim/colors 
cd ~/.vim/colors
git clone https://github.com/tomasr/molokai.git
cp ~/.vim/colors/molokai/colors/molokai.vim  ~/.vim/colors 
vim .vimrc

colorscheme molokai
 
"molokai设置
set t_Co=256
let g:molokai_original = 1 
let g:rehash256 = 1 
"代码颜色区分
syntax enable
syntax on
 
"tab宽度和缩进设置
set tabstop=4
set softtabstop=4
set shiftwidth=4
"兼容老版本
set nocompatible
set backspace=indent,eol,start
 
"自动缩紧和对齐
set autoindent
set smartindent
 
"开启追踪列表选择
set cscopetag
set hlsearch
 
"默认显示行号
set number
 
"自动加载和保存折叠
au BufWinLeave * silent mkview
au BufWinEnter * silent loadview
 
"括号和引号自动补全
"inoremap ' ''<ESC>i
"inoremap " ""<ESC>i
"inoremap ( ()<ESC>i
"inoremap [ []<ESC>i
"inoremap { {}<ESC>i
"GoLang代码提示
imap <F2> <C-x><C-o>
"开启NerdTree
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"autocmd VimEnter * NERDTree
let NERDTreeWinSize=30
"文件结构显示
nmap <F4> :TagbarToggle<CR>
"let g:tagbar_autopreview = 1 "开启自动预览(随着光标在标签上的移动,顶部会出现一个实时的预览窗口 
let g:tagbar_sort = 0 "关闭排序,即按标签本身在文件中的位置排序
let g:tagbar_left = 1 "默认在右边,设置在左边
let g:tagbar_width = 30 "宽度
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'
\ }
"go函数追踪 ctrl+]追踪 ctrl+o返回
autocmd FileType go nnoremap <buffer> gd :call GodefUnderCursor()<cr>
autocmd FileType go nnoremap <buffer> <C-]> :call GodefUnderCursor()<cr>
let g:godef_split=3    "追踪打开新tab
let g:godef_same_file_in_same_window=1    "函数在同一个文件中时不需要打开新窗口
"保存自动goimports
let g:go_fmt_command = "goimports"
let g:goimports = 1
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'
call vundle#end()
filetype plugin indent on
安装 vim go 工具,这里需要保证go工具命令目录在环境变量或者bin目录可以找到:

vim
:GoInstallBinaries
安装 vimrc 中 包含的 Plugin 插件:

vim
:PluginInstall
汇总一下快捷键命令:

  • F2:代码提示
  • F3:开启关闭NERDTree(也可以:NERDTree)
  • F4:右侧展示文件结构
  • Ctrl + ]/o:代码追踪/回退



【推荐】 一篇文章教你玩转vim神器写代码
【推荐】 [Centos]openvpn服务端的安装(easy-rsa3)
【推荐】 程序员工作时用到的“工具”有哪些?
【推荐】 Facebook、微信团队、Twitter、微软开源软件列表一览