Plugin https://github.com/fatih/vim-go
Tutorial https://github.com/fatih/vim-go-tutorial
Vimrc https://github.com/fatih/dotfiles/blob/master/vimrc
- File
:GoRun % - Package
:GoRun - Debug
:GoDebugStart
Shortkeys
autocmd FileType go nmap <leader>r <Plug>(go-run)- Package
:GoBuild
Auto save on make with
set autowriteError navigation
map <C-n> :cnext<CR>
map <C-m> :cprevious<CR>
nnoremap <leader>a :cclose<CR> - File
:GoTestfrom test or implementation - Function
:GoTestFunc - Compile test
:GoTestCompile
Shortkey to build go implementation or test
" run :GoBuild or :GoTestCompile based on the go file
function! s:build_go_files()
let l:file = expand('%')
if l:file =~# '^\f\+_test\.go$'
call go#test#Test(0, 1)
elseif l:file =~# '^\f\+\.go$'
call go#cmd#Build(0)
endif
endfunction
autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>:GoCoverage:GoCoverageClear:GoCoverageToggle:GoCoverageBrowser
Toggle coverage
autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle):GoFmt:GoImports- Struct add tags
:GoAddTags - Struct remove tags
:GoRemoveTags - Manually import package
:GoImport <package>- tab completion - Manually remove package
:GoDrop <package> - Import with alias
:GoImportAs <alias> <package>
- Inner function -
dif,yif,vifetc - Whole function -
daf,yaf,vafetc
Format on save
let g:go_fmt_autosave = 1Disable gofmt parse errors
let g:go_fmt_fail_silently = 1Goimports on save
let g:go_fmt_command = "goimports"Struct line split
Plug 'AndrewRadev/splitjoin.vim'- split
gS - join
gJ
Snippets (Keyword + TAB) reference
Plug 'SirVer/ultisnips'fnfmt.Println()fffmt.Printf()lnlog.Println()lflog.Printf()
- Between test and implementation
:GoAlternate - Go to definition
:GoDeforgdorctrl-] - Back from definition
:GoDefPoporctrl-t - Show navvigation stack
:GoDefStack - Clear navigation stack
:GoDefStackClear - File type and function declarations
:GoDecls - Directory tyle and function declarations
:GoDeclsDir - jump to next function
]] - jump to previous function
[[ 3]],d]],v]]
Needed for :GoDecls and :GoDeclsDir
Plug 'ctrlpvim/ctrlp.vim'Alternate commands
autocmd Filetype go command! -bang A call go#alternate#Switch(<bang>0, 'edit')
autocmd Filetype go command! -bang AV call go#alternate#Switch(<bang>0, 'vsplit')
autocmd Filetype go command! -bang AS call go#alternate#Switch(<bang>0, 'split')
autocmd Filetype go command! -bang AT call go#alternate#Switch(<bang>0, 'tabe'):GoDocorK:GoDocBrowser:GoInfofunction signature- Highlight identifiers
:GoSameIds - Clear highlight identifiers
:GoSameIdsClear - List files in a package
:GoFiles - List file dependencies
:GoDeps - Find references to identifier
:GoReferrers - Info for identifier
:GoDescribe - Interfaces a type implements
:GoImplements - Possible error types
:GoWhicherrs - Channel info
:GoChannelPeers - Show possible call targets of function call
:GoCallees - Show function call locations
:GoCallers :GoCallstack- Guru scope whole GOPATH
:GoGuruScope ...
Get info
autocmd FileType go nmap <Leader>i <Plug>(go-info)Automatically show function info
let g:go_auto_type_info = 1Function info update delay (ms)
set updatetime=100Automatically highlight identifiers
let g:go_auto_sameids = 1- Rename identifier
:GoRename <newname> - Show free variables
visual select + :GoFreevars
- Implement interface for type
:GoImpl <interface>
:GoLint:GoVet:GoMetaLinter
Meta linter checks for :GoMetaLinter
let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck', 'deadcode']Meta linter on save
let g:go_metalinter_autosave = 1Meta linter checks for autosave
let g:go_metalinter_autosave_enabled = ['vet', 'golint']let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_operators = 1
let g:go_highlight_extra_types = 1
let g:go_highlight_build_constraints = 1
autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4qShow how function is reached:GoCallstack
autocmd FileType go nmap <leader>q <Plug>(go-callstack)wAlternwate window splits
nnoremap <leader>w <C-w>wWOnly one active window
nnoremap <Leader>W :only<CR>eGenerate error handling:GoIfErr
autocmd FileType go nmap <leader>e <Plug>(go-iferr)rRename:GoRename[!] [to]
autocmd FileType go nmap <leader>r <Plug>(go-rename)tTest function:GoTestFunc[!] [expand]
autocmd FileType go nmap <leader>t <Plug>(go-test-func)` TTest file:GoTest[!] [expand]
autocmd FileType go nmap <leader>T <Plug>(go-test)yKeyify struct literals:GoKeyify
nnoremap <Leader>y :GoKeyify<CR>uFill struct with defaults:GoFillStruct
nnoremap <Leader>u :GoFillStruct<CR>iList interfaces implemented:GoImplements
autocmd FileType go nmap <leader>i <Plug>(go-implements)IStub interface methods:GoImpl [receiver] [interface]
nnoremap <Leader>I :GoImpl<CR>p cPossible callers of a function:GoCallers
autocmd FileType go nmap <leader>pc <Plug>(go-callers)p tPossible call targets for a type:GoCallees
autocmd FileType go nmap <leader>pt <Plug>(go-callees)p vPossible vars of a pointer:GoPointsTo
autocmd FileType go nmap <leader>pv <Plug>(go-pointsto)p ePossible error types of an error:GoWhicherrs
nnoremap <Leader>pe :GoWhicherrs<CR>p cChannel peers:GoChannelPeers
autocmd FileType go nmap <leader>pp <Plug>(go-channelpeers)aAlternate test/implementation:GoAlternate[!]
autocmd FileType go nmap <leader>a <Plug>(go-alternate-edit)sDescribe type or identifier:GoDescribe
autocmd FileType go nmap <leader>s <Plug>(go-describe)dShow doc:GoDoc [word]
autocmd FileType go nmap <leader>d <Plug>(go-doc)DBrowse doc:GoDocBrowser [word]
autocmd FileType go nmap <leader>D <Plug>(go-doc-browser)fShow all functions and types in file:GoDecls [file]
nnoremap <Leader>f :GoDecls<CR>FShow all functions and types in directory:GoDeclsDir [dir]
nnoremap <Leader>F :GoDeclsDir<CR>gShow definition jump stack:GoDefStack [number]
autocmd FileType go nmap <leader>g <Plug>(go-def-stack)GClear definition jump stack:GoDefStackClear
autocmd FileType go nmap <leader>G <Plug>(go-def-stack-clear)hShow all identifiers referring to the object:GoReferrers
autocmd FileType go nmap <leader>h <Plug>(go-referrers)HToggle highlight same identifiers:GoSameIdsToggle
nnoremap <Leader>H :GoSameIdsToggle<CR>lList buffers
nnoremap <leader>l :ls<CR>:b<space>LList files
nnoremap <Leader>L :Explore<CR>wClose window split
nnoremap <leader>x <C-w>ccToggle coverage:GoCoverageToggle[!] [options]
autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)CBrowse coverage:GoCoverageBrowser[!] [options]
nnoremap <Leader>C :GoCoverageBrowser<CR>vShow free variables in selection:GoFreevars
autocmd FileType go nmap <leader>v <Plug>(go-freevars)bBuild:GoBuild[!] [expand]:GoTestCompile[!] [expand]
autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>BRun:GoRun[!] [expand]
autocmd FileType go nmap <leader>B <Plug>(go-run)mMeta linter:GoMetaLinter [path]
autocmd FileType go nmap <leader>m <Plug>(go-metalinter)gdGo to definition:GoDef<C-t>Back from definition:GoDefPop [count]
nnoremap <leader><TAB> :b#<CR>
nnoremap <CR> :nohlsearch<CR><CR>