Based on the vim-go video by Fatih:
GoCoverageToggle: Amazing tool to check coverage per line of code. Already found it very useful in posting/list.go.
au FileType go nmap <Leader>c <Plug>(go-coverage-toggle)
Motions
If you are in a Go function, you can select the entire body using if
and af
. For e.g., vif
would visually select inside function, and vaf
would visually select the whole function including comment.
Automatic closing brackets
Another pretty useful thing is to avoid having to type closing brackets.
" Automatically close brackets.
Plugin 'jiangmiao/auto-pairs'
Convert single line struct defs to multiline
Also see: GitHub - AndrewRadev/splitjoin.vim: Switch between single-line and multiline forms of code
Snippets
err has a lot of variants
fn → fmt.Println(“”)
ff → fmt.Printf(“string(val) = %+v\n”, string(val))
JSON in structs
Say you start with this:
type foo struct {
Message string
Type string
ServerName string
}
Visually select the whole thing and call :GoAddTags
, and you get this.
type foo struct {
Message string `json:"message"`
Type string `json:"type"`
ServerName string `json:"server_name"`
}
Error checking
Use :GoErrCheck
, or :GoMetaLinter
. One can also set it up so tools like vet
or lint
are run automatically on save.
Navigation
:GoAlternate
toggles between test file and main go code.
Also check out the autocmd :AV
.
Documentation
Press K
on a function name which shows you the :GoDoc
.
let g:go_auto_type_info = 1
to always get the func signature.
Guru
Guru has a whole bunch of functions to make code browsing a lot easier. That’s a separate topic in itself.
Implement interfaces easily
This one is really interesting. You start off with
type bar struct{}
And then call :GoImpl sort.Interface
with your cursort pointed on bar
and this is what you get.
type bar struct{}
func (b *bar) Len() int {
panic("not implemented")
}
func (b *bar) Less(i int, j int) bool {
panic("not implemented")
}
func (b *bar) Swap(i int, j int) {
panic("not implemented")
}
:GoImpl
automatically generates the methods of the interface for you. Not only that, it also helps you with autocompletion of the interface names.
:GoPlay
automatically creates a link for your code on https://play.golang.org, and copies the url to clipboard.
That’s all for now! Highly recommend watching the full video by Fatih. Also recommend reading his tutorial: