Golang Development using the Vim-go plugin

Source: Internet
Author: User
Tags code tag gocode
This is a creation in Article, where the information may have evolved or changed.

Go (Golang) support for Vim, which comes with pre-defined sensible settings (like Auto gofmt on Save), with AutoComplete, Snippet support, improved syntax highlighting, go toolchain commands, and more. If needed vim-go installs all necessary binaries for providing seamless vim integration with current commands. It ' s highly customizable and each individual feature can disabled/enabled easily.

Features

  • Improved Syntax highlighting with items such as Functions, Operators, Methods.
  • Auto Completion Support viagocode
  • Better on gofmt save, which keeps cursor position and doesn ' t break your undo history
  • Go to Symbol/declaration with:GoDef
  • Look up documentation with :GoDoc inside Vim or open it in browser
  • Automatically import packages via :GoImport or plug it into AutoSave
  • Compile your package :GoBuild with, install it with :GoInstall or test them with :GoTest (also supports running single tests via )
  • Quickly execute your current file/files with:GoRun
  • Automatic GOPATH detection based on the directory structure (i.e. projects gb , godep vendored projects)
  • Change or display with GOPATH:GoPath
  • Create a coverage profile and display annotated source code to see which functions is covered with:GoCoverage
  • Call gometalinter :GoMetaLinter with, which invokes all possible linters (Golint, vet, Errcheck, Deadcode, etc ...) and shows the warnings/ Errors
  • Lint Your code with:GoLint
  • Run your code through to :GoVet catch static errors
  • Advanced Source analysis tools utilizing guru, such as :GoImplements , :GoCallees , and:GoReferrers
  • Precise Type-safe renaming of identifiers with:GoRename
  • List all source files and dependencies
  • Unchecked Error checking with:GoErrCheck
  • Integrated and improved snippets, supporting ultisnips orneosnippet
  • Share your current code to play.golang.org with:GoPlay
  • On-the-fly type information about the word under the cursor. Plug it into your custom vim function.
  • Go asm Formatting on save
  • Tagbar support to show tags of the source code in a sidebar withgotags
  • Custom Vim text objects such as a function or inner function list.
  • Jump to function or type declarations with :GoDecls or:GoDeclsDir
  • A Async launcher for the GO command was implemented for Neovim, fully async building and testing (Beta).
  • Integrated with the Neovim terminal, launch and other :GoRun go commands in their own new terminal. (Beta)
  • Alternate between implementation and test code with:GoAlternate

Checkout The official tutorial that goes literally over all features and shows many tips and tricks. It shows how to install Vim-go and explains many unknown use cases. Recommended for beginners as well as advanced users:https://github.com/fatih/vim-go-tutorial

Install

Master branch is supposed to be a development branch. So stuff here can break and change. Please try the latest release

Vim-go follows the standard runtime path structure, so I highly recommend to use a common and well known plugin manager to Install Vim-go. Vim-go with other go oriented vim plugins. For pathogen just clone the repo. For other plugin managers add the appropriate lines and execute the plugin ' s install command.

    • pathogen
      • git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go
    • vim-plug
      • plug ' fatih/vim-go '
    • neobundle
      • neobundle ' fatih/vim-go '
    • vundle
      • Plugin ' fatih/vim-go '
    • vim packages  (since V Im 7.4.1528)
      • git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go li>

Sure all necessary binaries is installed (such as,, gocode godef goimports etc.). You can easily install them with the included :GoInstallBinaries command. If invoked, all necessary binaries would be is automatically downloaded and installed to your $GOBIN environment (if not set it would use $GOPATH/bin ). Note that this command requires for git fetching the individual Go packages. Additionally, use to :GoUpdateBinaries update the installed binaries.

Optional

    • Autocompletion is enabled by default via <C-x><C-o> . To get real-time completion (completion by type) Install:neocomplete-Vim or Deoplete and deoplete-go for Neovim or Sim Pleautocmplpop
    • To display source code tag information on a sidebar install Tagbar.
    • For snippet features Install:neosnippet or ultisnips.
    • Screenshot color scheme is a slightly modified Molokai:fatih/molokai.
    • For a better documentation viewer Checkout:go-explorer.

Usage

Many of the plugin ' s features is enabled by default. There is no additional settings needed. All usages and commands is listed in doc/vim-go.txt . Note that help tags needs to be populated. Check your plugin manager settings to generate the documentation (some do it automatically). After a just open the help page to see all commands:

:help vim-go

Example Mappings

Vim-go has several <Plug> mappings which can is used to create custom mappings. Unless otherwise specified, none of these mappings is enabled by default. Here some examples you might find useful:

Run commands such as for the current go run file <leader>r go build go test <leader>b <leader>t with and respectively. Display beautifully annotated source code to see which functions is covered with <leader>c .

Au FileType go nmap <leader>r <Plug> (go-run) au FileType go nmap <leader>b <Plug> (go-build) au File Type go nmap <leader>t <Plug> (go-test) au FileType go nmap <leader>c <Plug> (go-coverage)

By default the mapping gd are enabled, which opens the target identifier in current buffer. You can also open the definition/declaration, in a new vertical, horizontal, or tab, for the word under your cursor:

Au FileType go nmap <leader>ds <Plug> (go-def-split) au FileType go nmap <leader>dv <Plug> ( go-def-vertical) au FileType go nmap <leader>dt <Plug> (go-def-tab)

Open the relevant godoc for the word under the cursor with an <leader>gd or open it vertically with<leader>gv

Au FileType go nmap <leader>gd <Plug> (go-doc) au FileType go nmap <leader>gv <Plug> ( go-doc-vertical)

Or Open the Godoc in browser

Au FileType go nmap <leader>gb <Plug> (go-doc-browser)

Show a list of interfaces which is implemented by the type under your cursor with<leader>s

Au FileType go nmap <leader>s <Plug> (go-implements)

Show type info for the word under your cursor with <leader>i (useful if you have disabled auto showing type info via g:go_auto_type_info )

Au FileType go nmap <leader>i <Plug> (go-info)

Rename The identifier under the cursor to a new name

Au FileType go nmap <leader>e <Plug> (go-rename)

More <Plug> mappings can is seen with :he go-mappings . Also These is just recommendations, you is free to create more advanced mappings or functions based on :he go-commands .

Settings

Below is some settings you might find useful. For the full list see :he go-settings .

By default syntax-highlighting for Functions, Methods and Structs are disabled. To change it:

Let g:go_highlight_functions = 1let G:go_highlight_methods = 1let G:go_highlight_fields = 1let G:go_highlight_types = 1let G:go_highlight_operators = 1let g:go_highlight_build_constraints = 1

Enable Goimports to automatically insert import paths instead of GOFMT:

Let G:go_fmt_command = "Goimports"

By default vim-go shows errors for the FMT command, to disable it:

Let g:go_fmt_fail_silently = 1

Disable Auto FMT on Save:

Let G:go_fmt_autosave = 0

Disable opening browser after posting your snippet to play.golang.org :

Let G:go_play_open_browser = 0

By default if :GoInstallBinaries is called, the binaries was installed to $GOBIN or $GOPATH/bin . To change it:

Let G:go_bin_path = expand ("~/.gotools") Let G:go_bin_path = "/home/fatih/.mypath"      "or give absolute path

Disable updating dependencies when installing/updating binaries:

Let G:go_get_update = 0
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.