golang

You are currently browsing the archive for the golang category.

I’ve decided recently to get serious about learning golang.  I’ve had a great time playing around with other peoples code (Docker and Kubernetes namely) and it’s time for me to learn the language so I can contribute more than bash scripts.  For better of for worse, I’ve decided to start coding on a CentOS box.  I have a couple of reasons for doing this…

-Its the Linux distro I’m most familiar with currently
-I need to get better at working in Linux.  More stick time on straight CLI can’t hurt.  I feel like jumping into a full blown IDE might be a bit premature for me and possibly allow me to miss some of the basics as well.
-I plan to run the code on Linux servers (I think…?)

Disclaimer: Im just getting started in golang.  If something I suggest below is wrong, please tell me!  Still learning here

Note: I really struggle with the language being called ‘go’.  So Im trying to call it golang everywhere I can.  It can seem like a bit much at times…

So let’s get started.  The goal of this post is to end up with a functioning development workstation that I can use for playing around with golang.  First things first, let’s install some of the tools we’ll need…

Now that golang is installed (that was easy right?) let’s do one of the most complicated pieces of the configuration, configuring the paths.  Regardless, the main goal here is to set the ‘GOPATH’ environmental variable.  Let’s first look and see what environment variables golang knows about currently…

image

So it doesn’t currently have a path for ‘GOPATH’.  Before I set one, let’s explain why this is needed.  Let’s take the classic ‘Hello World’ go coding example to use.  The code for that looks something like this…

Let’s put that in a file just called ‘hello.go’ and then try running the code…

image
Nice!  It worked.  So as you can see, the ‘run’ command tells the compiler to run the golang file you specify.  Now let’s try to build the file…

image
Above we built a binary file, and then ran it.  At this point, that’s a totally independent executable that’s not relying on having golang installed to run it.  Pretty slick huh?  So since things seem to be working, why do we need to change any paths?  We wont be able to cover all the details in this post, but suffice to say that ‘build’ and ‘run’ aren’t how you’ll always be compiling code in golang.  Golang uses something called ‘workspaces’ that boil down to a defined directory structure.  When building code in workspaces, golang needs a path to tell it where to look.  This is what we define as the ‘GOPATH’.  So let’s define that, and then talk a little bit about workspaces.

To define the path, I’ll edit my .bashrc file and add the following line…

So my .bashrc file now looks like this…

image

If we source the file, and then check our environmental variables we should see our defined ‘GOPATH”’…

image
Cool.  So now let’s talk about the golang workspace.  For now, let’s use this basic structure to define a workspace…

$GOPATH/
bin/    #where installed binaries go
pkg/    #installed packages
src/    #your code repositories

So let’s make the following directories…

To give you an idea of how the file structure is important, let’s move our hello world application into the workspace.  To do this, I’ll create a folder for it under the /src directory, move the ‘hello.go’ file into it, and then run a ‘go install’…

image
Ok – so what happened?  Go built an executable for us and put it in the bin directory for us…

image
It knew how to do this based on the ‘GOPATH’ environmental variable.  If this wouldn’t have been defined as we did above, we would have received an error like this…

image

Ok – So that’s about as far as I want to go with the golang stuff tonight, however, there is one other component I want to cover.  While I decided not to use a IDE, I can still get some pretty decent syntax tools for golang using VIM.  For instance, let’s take a look at what I get right now with the base VIM install…

image
Just sort of the basics right?  VIM can tell me where the opening and closing brackets/braces are but that’s about it.  So Im going to install a VIM plugin called ‘vim-go’.  To install the plugin, I’m going to use a VIM plugin manager called ‘Vundle’.

At this point, we’ve installed Vundle and we’ve told it to install the vim-go plugin.  Let’s load VIM now and tell it to install the plugins.  We do this by start VIM and using the VIM command ‘:PluginInstall’…

image

image
Now use the command ‘:bdelete’ to exit Vundle and go back to the main screen.  That’s it, the vim-go plugin is installed.  Let’s go back and checkout our hello.go file now…

image
Save the highlighting, it doesn’t look too different does it?  Let’s write the file (:wq) and then open it again…

image
Nice!  The plugin automatically reformats the code when we write the file.  We also get some other cool built in functions but I’ll cover those in greater detail in future posts.

Lastly, I also found this pretty awesome color scheme called molokai for VIM that I find makes coding easier.  Install is pretty easy…

So we’re up and running now, next step is to start writing some code and get it out on GitHub.  Hopefully more coming soon!

Tags: