The case for Go

Golang

I have a lot of code sitting on my harddisks, that's what I do for a living: a lot of thinking, some code experiment, and (arguably less) code out that enjoys the sunlight.

The tools, not the language

To achieve a decent level of productivity, most of the time I'm supposed to be using some form of IDE, and as a matter of fact, it works quite well, for that amount of daily fit into the big picture, take data here, send it there, convert this to that, persist somewhere.

Not one single language

I have been working a lot through the years with Java, and since I have accumulated over time a lot of libraries and common practices, I can easily say I'm quite productive with it and I can do almost anything with it. But looking at it carefully, you can clearly see that Java can never be the "one and only" language, because in the end, you will need that tiny shell script to launch the program the way you want. There's a lot you can actually do with a shell script, and whatever that is, I think I've done it and overdone it.

Writing to a database, calling webservices, backing up your data, build a private CA to sign certificates... everything can be done with a shell, provided you're not bound to death to a windowed system.

Two decades ago I used to get some work done this way in Perl (the most blamed programming language on planet earth) but that's the same thing: a glorified shell with proper hashtables and structures. And I was actually doing quite good with that: getting things done on cheap shared accounts while Java would have required expensive, dedicated physical machines.

The wind of change

But the world has changed quite a lot since then: Java is getting better, and machines are much cheaper.

The state of security is still what it is... but that's another story.

Python? Yes, you just have to get the right version and there's plenty of libraries to do almost everything. I really don't want to diminish it, since it's basically everywhere - and it works. But I think the idea of using indentation for control structures is quite unfortunate and.

But fast-forward from the aestetics of Python, the point is the same: I still see a lot of shell scripts calling Python programs.

What I like about python is that - compared with "the bloat", it is still entirely possible to code in the terminal. I've been traveling a lot in the last couple of years, and if you're sitting on a train and have some idea to experiment, I can't really figure out myself right clicking in some IDE and refactoring stuff, the only king in these circumstances is one: the terminal.

I recall when Golang was released: it has never looked too attractive to me, also the fact of having methods named with capital letters seemed to scream to me like a regexp in Java code. Also the binaries that it produces are huge.

Since I was thinking of it as a C replacemnt, it never picked up with me. But now I look at it for what you can get done (like I would sitting on a train with shell + curl + openssl + sqlite3) and it is basically a super-efficient and fast replacement for code that would otherwise be split between some high level code and a lot of "glue" scripting.

Plus: easy parallelism (goroutines)!

I think I have rationalized enough the fact that I've been recently enjoyng Go code, the real explanation is that I have figured out how to finally have auto-completion in the terminal, with Vim.

The fix

And there you have it. First thing first, it is honest to mention that most of it comes out of this post

Compile Vim

First thing first, before you start cursing at the Vim plugins, you need to install a Vim compiled with python3 support.

You either get it from some distribution (some say if you install gvim you get it already) or you have to compile it yourself.

In both cases, don't proceed until you can see a +python3 when you do:

 vim --version

To compile Vim:

git clone https://github.com/vim/vim.git
./configure --with-features=normal --enable-python3interp --enable-multibyte --disable-gui --prefix=/usr
make
sudo make install

If you get to see +python3 you can proceed from there.

Go auto-complete

Provided that you already have a working Go installation, all you need to do in order to have syntax completion for Go in Vi, is what follows:

mkdir -p ~/.vim/pack/plugins/start
git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go

cd ~/.vim/pack/plugins/start/vim-go
git checkout $(git tag -l --sort version:refname | grep -v rc | tail -1)

cat <<__EOL__ >>~/.vimrc
" GO Stuff
filetype plugin indent on
" Common Go commands
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage-toggle)
au FileType go nmap <Leader>e <Plug>(go-rename)
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)

" Navigation commands
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)

" Alternate commands
au FileType go nmap <Leader>ae <Plug>(go-alternate-edit)
au FileType go nmap <Leader>av <Plug>(go-alternate-vertical)

" Use new vim 8.2 popup windows for Go Doc
let g:go_doc_popup_window = 1

" Enable lsp for go by using gopls
let g:completor_filetype_map = {}
let g:completor_filetype_map.go = {'ft': 'lsp', 'cmd': 'gopls -remote=auto'}"

__EOL__

vim -c ":helptags ALL" -c ":q"
vim -c ":GoInstallBinaries" -c ":q"

Paolo Lulli 2021



[certificate] [security] [python] [java] [golang] [api] [git] [programming]