Vim and Go setup

I’m reading Learning Go by Jon Bodner. The opening chapters have some nice tips on getting a good developer environment. Vim has the excellent vim-go plugin that bundles a ton of nice stuff. Here are some notes I took while I got things setup.

My .vimrc is here.

My detailed notes from reading the book are here, and what follows below is just the subset that deals with dev environment.

Vi

Install vim-go: it comes with a massive set of tools.

goimports

goimports for some neat stuff that gofmt doesn’t do. Install with:

go install golang.org/x/tools/cmd/goimports@latest

And run with:

goimports -l -w .

Vi: Just save it.

Linting with golint

Install: go install golang.org/x/lint/golint@latest

Run: golint ./...

Vi: :GoLint

SA with govet

Run: go vet ./...

Vi: :GoVet

This does not catch subtle bugs around shadow variables. So consider installing shadow as well:

Install: go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest

Run: shadow ./...

Combine golint, govet with golangci-lint

This tool runs 10 different linters by default and support dozens others.

Install: see official docs

Run: golangci-lint run

vim-go notes

  • Code completion is with Ctrl-x Ctrl-o
  • :Tagbar is bound to F8
  • :GoDef is bound to gd
  • :GoDefStack shows you how deep you’ve jumped, GoDefPop or Ctrl-T just pops to the last hop

Sample makefile

.DEFAULT_GOAL := build

fmt:
        go fmt ./...
.PHONY:fmt

lint: fmt
        golint ./...
.PHONY:lint

vet: fmt
        go vet ./...
        shadow ./...
.PHONY:vet

build: vet
        go build hello.go
.PHONY:build

Bash history substitution trick

I saw the following neat little trick in this link:

# renaming a long file-name using a regex
$ touch config.txt
$ mv config.txt !#:1:s/txt/json

The exact meaning of the !# is, quoting from the man page:

Event Designators       An event designator is a reference to a command line entry in the history list.  Unless the reference is absolute, events are relative to the current position in the history list.
       !      Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). 

<snipped>

 !#     The entire command line typed so far.

From ‘man bash’

i.e. most people know that bash can insert things from history using commands like !! and !50 (i.e. rerun last command and rerun the 50th history item respectively).

But !# let’s you take something from the existing line and re-use it.

Here is another example, from here:

curl http://beyondgrep.com/ack-2.14-single-file > ~/bin/ack && chmod 0755 !#:3 

In this case, !#:3 takes the 3rd element from the existing command. Starting from zero, that turns out to be ~/bin/ack in this case.

git submodule usage

Update: Looks like subtrees are a nicer alternative to submodules. Anyway..

  • To update all submodules:
git submodule update --init --recursive
  • To fetch the latest code from a submodule:
    cd <submodule-folder>
    git pull
    cd ..
    git commit -am "bumping up submodule version"

Then merge the code. The next time the parent repository is pulled, updating the submodule will get the latest commit in it.