golang up and running on CentOS7 – take two

After some great feedback and some additional learning/fixes on my end, I wanted to make an updated version of this post. 

This go around, I’ve added some plugins I found helpful as well as made a couple of tweaks that I think (not sure yet) will be helpful to me going forward.  So here is the brand new build script I came up with…

#Install dependancies and neccessary packages
yum -y install golang git vim wget python-devel cmake
yum -y groupinstall "Development Tools"

#Modify your bash_profile...
vim ~/.bash_profile
#Add this config...
export GOPATH=$HOME/go
#Source the file
source .bash_profile

#Make the golang workspace
mkdir ~/go
mkdir ~/go/bin
mkdir ~/go/pkg
mkdir ~/go/src

#Install and configure Vundle...
#Pull down Vundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
#Edit your .vimrc file...
vim ~/.vimrc
#Add this config...
set nocompatible
filetype off
colorscheme molokai
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#rc()
Plugin 'gmarik/Vundle.vim'
Plugin 'nsf/gocode', {'rtp': 'vim/'}
Plugin 'fatih/vim-go'
Plugin 'Valloric/YouCompleteMe'
Plugin 'scrooloose/nerdtree.git'
filetype plugin indent on
"Prevent autocomplete help from staying visisble
autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
"Quit VIM if NERDTree is last open Window
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
"Autoload NERDTree on VIM start
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
"Beginning and end of line shortcuts
map  
map  

#Pull down the vim color scheme
mkdir ~/.vim/colors
wget https://raw.githubusercontent.com/fatih/molokai/master/colors/molokai.vim -O ~/.vim/colors/molokai.vim

#Start vim and install the plugins
:PluginInstall

#Wait for the install to complete and exit VIM
:bd
:q

#Start VIM and download the golang binaries
:GoInstallBinaries

#Exit VIM and Build YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe/
./install.sh

Ok – So that’s really not that bad.  We’ve added some new stuff here so I want to walk through that.  Let’s do that and cover some VIM basics at the same time.

Vim can be a rather frustrating experience if you don’t know what you’re doing and get stuck.  So let’s open up vim and edit a file called ‘test.go’.  You should get a screen that looks like this…

image
Huh – So not what you’re used to seeing right?  We installed a plugin called NERDTree and part of the vimrc config was to load it on default.  Since it does that, we now have two windows or what vim calls ‘splits’.  So essentially we have two windows open now.  One with NERDTree and one with our regular editor.  To move between the windows we can hit ‘Ctrl+ww’.  Before you do that, try just typing something in the left window.  Then cycle over to the NerdTree using the change window shortcut we used above.  Arrow down to the ‘anaconda-ks.cfg’ file and hit enter on it…

image 
Now we have three windows open.  To close a window, we can use the buffer delete command of ‘:bd’.  Let’s close the file we just opened by deleting it’s buffer.  If you had made changes to the buffer (in this case, buffer and window are the same thing), vim would prompt you that you had changes and needed to save them or ignore them.

So let’s go back to NERDTree.  Cycle over to that window, arrow down to select the ‘go’ folder, and then hit the ‘O’.  This will recursively open all the folders beneath go and you should end up with this…

image 
‘X’ will recursively close the structure.  Other helpful keys are…

‘C’ – Make the selected directory the new root
’e’ – Explore the selected directory in a new window
’i’ – Open selected file in new split

So that’s a pretty handy plugin.  If you don’t want it to load on vim start, modify the vimrc accordingly.  If you don’t start it on load, you can start it at anytime with the ‘:NERDTree’ command from within vim. 

The next thing we have is syntax checking and auto completion.  Check this out.  Start typing this example code…

image
Wow!  So yeah, we officially have auto complete now.  Pretty slick huh?  Also we get syntax checking too.  Try writing the file right now to see what happens…

image 
Now if you hit enter, it will split the window and highlight the line it doesn’t like…

Note: You may need to cycle back to the right window if NERDTree is selected now

image
When you’ve fixed the issue, you can go ahead and try to write the file again.  If the syntax passes it should close, if not, you’ll get the same syntax warning.  If you really don’t want to fix it right now you can write the file and exit with a ‘:wqa’

The other plugins provide a whole lot of other cool stuff related to golang but we wont see that until we start coding so I’m going to hold off for now.  Hoping my next post here will be about actually writing golang now that I have this sorted out.

Thanks for reading!

Leave a Reply

Your email address will not be published.