/ code

Programming with Go: Installation

Welcome to The rollBak's Programming with Go Code-Along! This is going to be a structured, in-depth series that covers the Go programming language. We're going to start with the bare minimum of basics, walking through the installation process and getting familiar with the language. After that, we're going to stand up our own web server and web site using Golang.


Previous programming experience will be useful, but it is not a requirement to follow along. Definitely let us know if you have any questions in the comments or on Twitter.


This first part is dedicated wholly to installation. If you do not need assistance with installation or if you already have Go setup, feel free to skip this page.


The Setup


Due to the nature of programming and the variety of people it attracts, I'm going to spend some time going through the setup of a Go programming environment on several workstations. Click the link below associated with your operating system, or, feel free to scroll on through and critique my work.

Windows

Fedora

MacOS

Ubuntu


Windows



Windows has two ways to primarily download and install Go.

The Download


The first, visit the website, download the .msi package for Windows, and begin the installation. Go through the installation, and you're all set!

Well, sort of.


You'll probably want to setup some environment variables.


The first is the Go binary itself. If you open PowerShell and type 'go version' you'll likely get an error:



So, head on over to your advanced system preferences. If you have a "Pause/Break" key, you can press Windows + Pause/Break to bring up the menu and click Advanced system settings. If you don't have the key, you can press Windows + R, type control,  select "System" (you may have to change the view to small or large icons to see this option), and click Advanced system settings.


From there, you want to click "Environment Variables" > Select Path under "System variables" > Select New > Enter C:\Go\bin (or wherever you installed the system)

Click "OK" on all of the settings menus and open a new PowerShell terminal. Do not use one that's already open, make sure you open a new terminal window.
Now, typing 'go version' shows the correct output:


If you still receive an error, make sure there is not a typo in the PATH, make sure you opened a new shell, and that you closed all of the system windows. If you still receive an error, check the path and make sure it exists. If not, it may have been installed elsewhere.

Chocolatey Package Manager


Chocolatey is a package manager for Windows similar to Homebrew on OS X or apt on Debian. If you're not familiar with it but are interested in development on Windows, I highly recommend installing it.


The installation is simple. Open up PowerShell as Administrator and enter:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))


If you don't trust the command, you can visit the Chocolatey installation page for more information.


Once that's done, to install Golang is one simple command:

choco install golang -y


The -y is optional, it's just to answer "Yes" to the questions about package installation.


When the command completes, open a new shell and type 'go version'. You should see the expected output:



Golang Workspace


We're almost home! Visit your home directory at C:\Users\userName\ and check to see if there is a go directory there. If not, go ahead and create one. C:\Users\userName\go should be the target. Make a src directory inside of there. Go ahead and make a directory called "Projects" as well.


The full path will be:

C:\Users\userName\go\src\Projects


Inside of your Projects directory, make a main.go file. I'm using VS Code for my editor, because they have some awesome Go tools!


Open up main and add the following:

package main
import (    
    "fmt"
)

func main() {    
    fmt.Println("Testing Golang")
}


Depending on the editor and tools you have installed, when you save that file it might make some changes. That's fine, we'll add to this file later.
Open up PowerShell and enter 'go run Projects'
You should see "Testing Golang" spit out on your terminal:


This means your workspace is setup correctly. In the image above, I was in the Projects directory. But you don't have to be. Go ahead and try running the above command from anywhere in your system.


If you receive an error, check to make sure Go is setup correctly. Worst case scenario, you may have to reinstall the software under C:\Go


But, that's all it takes for Windows. You can create any directory you want. If you want to have a C:\Users\userName\go\src\Projects\therollBakCodeAlong, you can do that too. Just add that to your go run path:

go run Projects/therollBakCodeAlong



Congrats! You have a full functional Go environment on Windows. And who says you can't code on Windows?



Fedora


Due to the nature of Fedora, the installation for Golang is quite simple:

sudo dnf install golang -y


Fedora offers the latest packages, so with that command you'll have the current release of Go:


You can enter 'go version' to make sure the latest version of Go is installed on your computer. However, a little more work is involved to setup your Go path.


Open your ~/.bashrc file and add two lines:

export PATH=$PATH:$(go env GOPATH)/bin
export GOPATH=$(go env GOPATH)


This exact location isn't required, you can have your Go workspace anywhere. This falls under the best practices, however. You can read more about that here.


Save the ~/.bashrc file and enter

. ~/.bashrc

to load the new environment variables.


Now, we can create our Projects directory:

mkdir -p go/src/Projects


We can now create our test file to make sure everything is working.


I am using Vim, but you can use whatever editor you prefer, be it Atom, VS Code, Sublime Text, etc.

vim go/src/Projects/main.go


Add the following code into the file:

package main

import (
    "fmt"
)

func main() {    
    fmt.Println("Testing Golang!")
}


Save the file and run the following command to test your Go programming environment with 'go run Projects'. You should see the following output:


Congrats! You have setup a Go programming environment on Fedora! This skill will take you far in life, so remember where you came from when you make it to the big time.



MacOS


I'm going to take a copout here and go the Homebrew route. I'm assuming if you have an OS X operating system and you're reading this, you're pretty keen on development and have Homebrew installed. If not, go ahead and run this in your terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"



The source for this command can be found here.


Once Homebrew is installed, you can install the latest version of Go with 'brew install golang'.


After that, make sure all is well with 'go version':


You'll want to edit your ~/.bash_profile with the following lines:

export PATH=$PATH:$(go env GOPATH)/bin
export GOPATH=$(go env GOPATH)


If you're using zsh or have a ~/.bash_aliases feel free to add those lines to that file instead.


Source the profile to export the environment variables:

. ~/.bash_profile


From there, you'll want to create your workspace:

mkdir -p $GOPATH/src/Projects


Using your favorite editor, create a main.go file and enter the following code:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Testing Go on OS X!")
}


In your terminal, run 'go run Projects'


You should see the following output:



Alright, you're all set! Stand by for the next tutorial to kick your Go skills into overdrive!



Ubuntu


Ubuntu is one of the most popular Linux distributions. It is used by engineers, developers, and Linux enthusiasts all over. It was even used at Google during the development of Go!


To get Go running, go ahead and pull the Golang binaries, extract the file, and move them into a location in your $PATH. You can opt to place the code elsewhere (such as /opt), I am going based on the Golang best practices.

curl -O https://storage.googleapis.com/golang/go1.11.2.linux-amd64.tar.gz
tar -xvf go1.11.2.linux-amd64.tar.gz
sudo mv go /usr/local/


Now, once you have the source in the desired location, you'll want to add some entries into your ~/.bashrc or ~/.bash_aliases file:

export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin


Once you have those entries, save and source the file.

. ~/.bashrc


After that, you should be good to create your workspace!

mkdir -p go/src/Projects


Use your favorite editor and create our first program:

package main

import (    
    "fmt"
)

func main() {    
    fmt.Println("Hello, Linux!")
}


Once that file is saved, enter: 'go run Projects'


You should see:



You're all set!




That wraps up the Go installation process. Whatever system you are armed with, you are ready for battle. Next, we're going to cover the basics: Variables, types, decision, loops, arrays, and functions. It might be a multiple part series before we move into some of the advanced concepts like structs, reading and writing to files, and web servers.

Programming with Go: Installation
Share this