# VIM-GO Plugin `https://github.com/fatih/vim-go` Tutorial `https://github.com/fatih/vim-go-tutorial` Vimrc `https://github.com/fatih/dotfiles/blob/master/vimrc` ## Run ### Commands * File `:GoRun %` * Package `:GoRun` * Debug `:GoDebugStart` ### vimrc Shortkeys ``` autocmd FileType go nmap r (go-run) ``` ## Build ### Commands * Package `:GoBuild` ### vimrc Auto save on make with ```set autowrite``` Error navigation ``` map :cnext map :cprevious nnoremap a :cclose ``` ## Test ### Commands * File `:GoTest` from test or implementation * Function `:GoTestFunc` * Compile test `:GoTestCompile` ### vimrc 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 b :call build_go_files() ``` ## Coverage ### Commands * `:GoCoverage` * `:GoCoverageClear` * `:GoCoverageToggle` * `:GoCoverageBrowser` ### vimrc Toggle coverage ``` autocmd FileType go nmap c (go-coverage-toggle) ``` ## Edit ### Commands * `:GoFmt` * `:GoImports` * Struct add tags `:GoAddTags` * Struct remove tags `:GoRemoveTags` * Manually import package `:GoImport ` - tab completion * Manually remove package `:GoDrop ` * Import with alias `:GoImportAs ` ### Text objects * Inner function - `dif`, `yif`, `vif` etc * Whole function - `daf`, `yaf`, `vaf` etc ### vimrc Format on save `let g:go_fmt_autosave = 1` Disable gofmt parse errors `let g:go_fmt_fail_silently = 1` Goimports on save `let g:go_fmt_command = "goimports"` Struct line split `Plug 'AndrewRadev/splitjoin.vim'` * split `gS` * join `gJ` Snippets (Keyword + TAB) `Plug 'SirVer/ultisnips'` [reference](https://github.com/fatih/vim-go/blob/master/gosnippets/UltiSnips/go.snippets) * `fn` fmt.Println() * `ff` fmt.Printf() * `ln` log.Println() * `lf` log.Printf() ## Navigation ### Commands * Between test and implementation `:GoAlternate` * Go to definition `:GoDef` or `gd` or `ctrl-]` * Back from definition `:GoDefPop` or `ctrl-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]]` ### vimrc Needed for `:GoDecls` and `:GoDeclsDir` `Plug 'ctrlpvim/ctrlp.vim'` Alternate commands ``` autocmd Filetype go command! -bang A call go#alternate#Switch(0, 'edit') autocmd Filetype go command! -bang AV call go#alternate#Switch(0, 'vsplit') autocmd Filetype go command! -bang AS call go#alternate#Switch(0, 'split') autocmd Filetype go command! -bang AT call go#alternate#Switch(0, 'tabe') ``` ## Documentation ### Commands * `:GoDoc` or `K` * `:GoDocBrowser` * `:GoInfo` function 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 cunction call `:GoCallees` * Show function call locations `::GoCallers` * `:GoCallstack` * Guru scope whole GOPATH `:GoGuruScope ...` ### vimrc Get info `autocmd FileType go nmap i (go-info)` Automatically show function info `let g:go_auto_type_info = 1` Function info update delay (ms) `set updatetime=100` Automatically highlight identifiers `let g:go_auto_sameids = 1` ## Refactor ### Commands * Rename identifier `:GoRename ` * Show free variables `visual select + :GoFreevars` ## Codegen ### Commands * Implement interface for type `:GoImpl ` ## Checks ### Commands * `:GoLint` * `:GoVet` * `:GoMetaLinter` ### vimrc Meta linter checks for `:GoMetaLinter` `let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck', 'deadcode']` Meta linter on save `let g:go_metalinter_autosave = 1` Meta linter checks for autosave `let g:go_metalinter_autosave_enabled = ['vet', 'golint']` ## Syntax highlighting ### vimrc * `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` * tab size `autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4 ` ## Shortkey suggestion ### " Leader keys #### " Q "`q` Show how function is reached `:GoCallstack` `autocmd FileType go nmap q (go-callstack)` #### " W "`w` Alternwate window splits `nnoremap w w` "`W` Only one active window `nnoremap W :only` #### " E "`e` Generate error handling `:GoIfErr` `autocmd FileType go nmap e (go-iferr)` #### " R "`r` Rename `:GoRename[!] [to]` `autocmd FileType go nmap r (go-rename)` #### " T "`t` Test function `:GoTestFunc[!] [expand]` `autocmd FileType go nmap t (go-test-func)` "`T` Test file `:GoTest[!] [expand]` `autocmd FileType go nmap T (go-test)` #### " Y "`y` Keyify struct literals `:GoKeyify` `nnoremap y :GoKeyify` #### " U "`u` Fill struct with defaults `:GoFillStruct` `nnoremap u :GoFillStruct` #### " I "`i` List interfaces implemented `:GoImplements` `autocmd FileType go nmap i (go-implements)` "`I` Stub interface methods `:GoImpl [receiver] [interface]` `nnoremap I :GoImpl` #### " O #### " P "`p c` Possible callers of a function `:GoCallers` `autocmd FileType go nmap pc (go-callers)` "`p t` Possible call targets for a type `:GoCallees` `autocmd FileType go nmap pt (go-callees)` "`p v` Possible vars of a pointer `:GoPointsTo` `autocmd FileType go nmap pv (go-pointsto)` "`p e` Possible error types of an error `:GoWhicherrs` `nnoremap pe :GoWhicherrs` "`p c` Channel peers `:GoChannelPeers` `autocmd FileType go nmap pp (go-channelpeers)` #### " A "`a` Alternate test/implementation `:GoAlternate[!]` `autocmd FileType go nmap a (go-alternate-edit)` #### " S "`s` Describe type or identifier `:GoDescribe` `autocmd FileType go nmap s (go-describe)` #### " D "`d` Show doc `:GoDoc [word]` `autocmd FileType go nmap d (go-doc)` "`D` Browse doc `:GoDocBrowser [word]` `autocmd FileType go nmap D (go-doc-browser)` #### " F "`f` Show all functions and types in file `:GoDecls [file]` `nnoremap f :GoDecls` "`F` Show all functions and types in directory `:GoDeclsDir [dir]` `nnoremap F :GoDeclsDir` #### " G "`g` Show definition jump stack `:GoDefStack [number]` `autocmd FileType go nmap g (go-def-stack)` "`G` Clear definition jump stack `:GoDefStackClear` `autocmd FileType go nmap G (go-def-stack-clear)` #### " H "`h` Show all identifiers referring to the object `:GoReferrers` `autocmd FileType go nmap h (go-referrers)` "`H` Toggle highlight same identifiers `:GoSameIdsToggle` `nnoremap H :GoSameIdsToggle` #### " J #### " K #### " L "`l` List buffers `nnoremap l :ls:b` "`L` List files `nnoremap L :Explore` #### " Z #### " X "`w` Close window split `nnoremap x c` #### " C "`c` Toggle coverage `:GoCoverageToggle[!] [options]` `autocmd FileType go nmap c (go-coverage-toggle)` "`C` Browse coverage `:GoCoverageBrowser[!] [options]` `nnoremap C :GoCoverageBrowser` #### " V "`v` Show free variables in selection `:GoFreevars` `autocmd FileType go nmap v (go-freevars)` #### " B "`b` Build `:GoBuild[!] [expand]` `:GoTestCompile[!] [expand]` `autocmd FileType go nmap b :call build_go_files()` "`B` Run `:GoRun[!] [expand]` `autocmd FileType go nmap B (go-run)` #### " N #### " M "`m` Meta linter `:GoMetaLinter [path]` `autocmd FileType go nmap M (go-metalinter)` #### " Extra "`gd` Go to definition `:GoDef` "`` Back from definition `:GoDefPop [count]` `nnoremap :b#` `nnoremap :nohlsearch`