Writing Your First Program with Go

AnnMargaret Tutu
Feb 7, 2017 · 3 min read

Go is a compiled, statically typed programming language created by Google. If your into Ethereum and blockchain technology, like I am, then you might have an interest in Go to get a better handle on Geth software and launching nodes.

I won’t get into Geth nodes in this tutorial, but I will share my experience with installing Go for Mac OS to help others get started.

Step One: Visit GoLang.Org

To install Go, head over to golang.org, and download the package file. (Note: if you’re using an Ubuntu setup, you can install Go with the sudo apt command).

Then, open the package and follow the installer’s prompts.

This will automatically install Go to the /usr/local path.

Step Two: Make Sure Go Installed Properly

The quick way of checking whether you have go installed: open up a terminal window and type in “go version”.

Step Three: Create and Register Your First Go Workspace

To make a learning experience out of it, I went ahead and built a very simple “hello world” app, as recommended by the documentation, to see if Go installed properly. Here are the details:

  • create a directory to store a workspace/quick sample application in the terminal. I made a directory called “go4swarmtutorial” on my Desktop (but you can name and put your workspace anywhere you’d like).
  • from the home directory (~), point the GOPATH environment variable to your new workspace and transcribe directions to said workspace in a file called .bash_profile (the shell startup script).

You’ll notice the exported path below all that Python mumbo-jumbo:

Step Four: Refresh Terminal and Create File Structure

Next, we went to close out and reopen the terminal, so we can reap the rewards of a fresh new startup with everything installed, and build out a barebones file structure for the workspace:

  • echo the $GOPATH variable to make sure it registered the right way
  • navigate to the new workspace (for me, “cd Desktop”, then “cd go4swarmtutorial”)
  • create four nested directories in this order:

(1) src

(2) github.com

(3) GithubUsername

(4) hello

It’s good practice to get into the habit of setting up your workspaces to push to github. I also like to have my favorite text editor, Sublime, open side-by-side with iTerm, so I can watch these folders generate in real time.

Step Five: Write the Hello World App

And with that last command — “touch hello.go” — we create a Go source file that gives us a main entry point and packaged logic to execute for this sample app (or say, if we wanted to import into a larger application):

Once we save our source code, we can compile our program by running the command “go run hello.go” and see our app in action:

There’s so much more to learn here, but this is a great entry point to get you excited! Documentation continues to abound on the web, and I will contribute with tutorials as I explore new areas of interest in the language.

Hope this helps (even if your using a different OS). If you have any questions, I’ll try to answer to the best of my abilities!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store