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 autowrite
Error navigation
map <C-n> :cnext<CR>
map <C-m> :cprevious<CR>
nnoremap <leader>a :cclose<CR>
- File
:GoTest
from 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
,vif
etc - Whole function -
daf
,yaf
,vaf
etc
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
fn
fmt.Println()ff
fmt.Printf()ln
log.Println()lf
log.Printf()
- Between test and implementation
:GoAlternate
- Go to definition
:GoDef
orgd
orctrl-]
- Back from definition
:GoDefPop
orctrl-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')
:GoDoc
orK
: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 ...
Get info
autocmd FileType go nmap <Leader>i <Plug>(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
- 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 = 1
Meta 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
- tab size
autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4
"q
Show how function is reached :GoCallstack
autocmd FileType go nmap <leader>q <Plug>(go-callstack)
"w
Alternwate window splits
nnoremap <leader>w <C-w>w
"W
Only one active window
nnoremap <Leader>W :only<CR>
"e
Generate error handling :GoIfErr
autocmd FileType go nmap <leader>e <Plug>(go-iferr)
"r
Rename :GoRename[!] [to]
autocmd FileType go nmap <leader>r <Plug>(go-rename)
"t
Test function :GoTestFunc[!] [expand]
autocmd FileType go nmap <leader>t <Plug>(go-test-func)
"T
Test file :GoTest[!] [expand]
autocmd FileType go nmap <leader>T <Plug>(go-test)
"y
Keyify struct literals :GoKeyify
nnoremap <Leader>y :GoKeyify<CR>
"u
Fill struct with defaults :GoFillStruct
nnoremap <Leader>u :GoFillStruct<CR>
"i
List interfaces implemented :GoImplements
autocmd FileType go nmap <leader>i <Plug>(go-implements)
"I
Stub interface methods :GoImpl [receiver] [interface]
nnoremap <Leader>I :GoImpl<CR>
"p c
Possible callers of a function :GoCallers
autocmd FileType go nmap <leader>pc <Plug>(go-callers)
"p t
Possible call targets for a type :GoCallees
autocmd FileType go nmap <leader>pt <Plug>(go-callees)
"p v
Possible vars of a pointer :GoPointsTo
autocmd FileType go nmap <leader>pv <Plug>(go-pointsto)
"p e
Possible error types of an error :GoWhicherrs
nnoremap <Leader>pe :GoWhicherrs<CR>
"p c
Channel peers :GoChannelPeers
autocmd FileType go nmap <leader>pp <Plug>(go-channelpeers)
"a
Alternate test/implementation :GoAlternate[!]
autocmd FileType go nmap <leader>a <Plug>(go-alternate-edit)
"s
Describe type or identifier :GoDescribe
autocmd FileType go nmap <leader>s <Plug>(go-describe)
"d
Show doc :GoDoc [word]
autocmd FileType go nmap <leader>d <Plug>(go-doc)
"D
Browse doc :GoDocBrowser [word]
autocmd FileType go nmap <leader>D <Plug>(go-doc-browser)
"f
Show all functions and types in file :GoDecls [file]
nnoremap <Leader>f :GoDecls<CR>
"F
Show all functions and types in directory :GoDeclsDir [dir]
nnoremap <Leader>F :GoDeclsDir<CR>
"g
Show definition jump stack :GoDefStack [number]
autocmd FileType go nmap <leader>g <Plug>(go-def-stack)
"G
Clear definition jump stack :GoDefStackClear
autocmd FileType go nmap <leader>G <Plug>(go-def-stack-clear)
"h
Show all identifiers referring to the object :GoReferrers
autocmd FileType go nmap <leader>h <Plug>(go-referrers)
"H
Toggle highlight same identifiers :GoSameIdsToggle
nnoremap <Leader>H :GoSameIdsToggle<CR>
"l
List buffers
nnoremap <leader>l :ls<CR>:b<space>
"L
List files
nnoremap <Leader>L :Explore<CR>
"w
Close window split
nnoremap <leader>x <C-w>c
"c
Toggle coverage :GoCoverageToggle[!] [options]
autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)
"C
Browse coverage :GoCoverageBrowser[!] [options]
nnoremap <Leader>C :GoCoverageBrowser<CR>
"v
Show free variables in selection :GoFreevars
autocmd FileType go nmap <leader>v <Plug>(go-freevars)
"b
Build :GoBuild[!] [expand]
:GoTestCompile[!] [expand]
autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>
"B
Run :GoRun[!] [expand]
autocmd FileType go nmap <leader>B <Plug>(go-run)
"m
Meta linter :GoMetaLinter [path]
autocmd FileType go nmap <leader>M <Plug>(go-metalinter)
"gd
Go to definition :GoDef
"<C-t>
Back from definition :GoDefPop [count]
nnoremap <leader><TAB> :b#<CR>
nnoremap <CR> :nohlsearch<CR><CR>