Thursday, September 18, 2014

Go on Ubuntu


Intro

I have a strong Java background and have messed around with various other languages but never become fluent in any of them.  I guess because I didn't have to.  Perl crossed my path early in my career, then Ruby became popular and later python.
Now I'm looking at Go.  Actually it's the third time or so I'm doing that, and this time there seems to be a click.  What attracts me most is that it's fairly easy to pick up, and... it generates a single executable that can exist on its own without having to install a run-time environment.  I assume that's one of the reasons why it's so popular in the cloud community these days (yes, next to python of course).
Anyway, just a quick write-up so I don't forget how to quickly get started with Go after my next PC refresh.  There's nothing new here... just summarizing few key steps in one place.

The basics

Check the downloads section in the golang website for the latest and download it.

$ cd /tmp
$ wget --no-check-certificate https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.3.1.linux-amd64.tar.gz

Create a workspace where you want to keep your Go projects

$ cd ~
$ mkdir -p ./workspace/go/src
$ mkdir -p ./workspace/go/bin

Add these lines to your ~/.bashrc file

export PATH=$PATH:/usr/local/go/bin
export GOROOT=$HOME/workspace/go
export PATH=$PATH:/$GOROOT/bin

Syntax highlighting and autocomplete in vi

Once there's good information on various tooling integration in /usr/local/go/misc.  I'm interested in vi.  Based on what's there in /usr/local/go/misc/readme.txt, here is the content of my /etc/vim/vimrc.local file.

" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to reload them.
if exists("g:did_load_filetypes")
  filetype off
  filetype plugin indent off
endif
set runtimepath+=/usr/local/go/misc/vim " replace $GOROOT with the output of: go env GOROOT
filetype plugin indent on
syntax on

" Reformat on exit/write of file.
autocmd FileType go autocmd BufWritePre <buffer> Fmt

" Close auto-complete window when exiting edit/insert mode.
autocmd InsertLeave * if pumvisible() == 0|pclose|endif

" Auto-complete braces
inoremap {      {}<Left>
inoremap {<CR>  {<CR>}<Esc>O
inoremap {     {
inoremap {}     {}

inoremap (      ()<Left>
inoremap (<CR>  (<CR>)<Esc>O
inoremap (     (
inoremap ()     ()

inoremap [      []<Left>
inoremap [<CR>  [<CR>]<Esc>O
inoremap [     [
inoremap []     []


And that's it.  You should now see syntax highlighting in vi.  Ctrl+x, ctrl+o opens up auto-complete.

Updated! Initial version of this post had some issues (<> tags where missing)

No comments:

Post a Comment