In this section, you will setup your Go development environment. Upon completion of this section you should have achieved the following:
- Installed the Go language for your OS.
- Cloned this repository.
- Configured the tutorial as your Go development environment.
- Installed the Glide package manager.
- Installed the IntelliJ Go plugin. If JetBrains products are not your thing, alternatives are provided.
- Opened your first Go project.
-
Download and install the latest 1.6.x version of Go for your OS from the Go download page.
-
Navigate to the location you downloaded your OS installer to and double-click on the installer.
-
Open a terminal/cmd window and type in
go version
and press enter. You should see output similar to the following:go version go1.6 darwin/amd64
That’s it! You are done installing Go for your OS.
If you are running Ubuntu, the easiest way to install a recent version of Go is through the “Ubuntu containers team” lxd-stable PPA.
-
Open a terminal window.
-
Type in
sudo apt-add-repository ppa:ubuntu-lxc/lxd-stable
and press enter. -
Press enter when prompted to accept adding the lxd-stable repository.
-
Type in
sudo apt-get update
and press enter. -
Type in
sudo apt-get install golang
and press enter. -
Open up a terminal/cmd window and type in
go version
and press enter. You should see output similar to the following:go version go1.6 linux/amd64
That’s it! You are done installing Go for Ubuntu Linux.
- Download the tarball of the 1.6.x version of Go for Linux from the Go download page.
- Follow the installation instructions for Linux found at the Go installation documentation page
Next you will clone this tutorial repository.
- Open a terminal/cmd window and navigate to the directory from which you would like to clone this repository.
- Type in
git clone https://github.com/rawlink/golang-intro
and press enter.
Now that you have cloned the tutorial repository, you will configure it as your Go development environment. It is assumed that you know how to configure environment variables for your OS. You may need to adjust the examples for your particular OS.
- Point the GOPATH variable at the cloned tutorial directory. e.g.
GOPATH=/home/rawlink/golang-intro
- Set the GO15VENDOREXPERIMENT environment variable to 1. e.g.
GO15VENDOREXPERIMENT=1
- Add the $GOPATH/bin directory to your PATH. Don’t worry if this directory doesn’t exist, Go will create it for you. e.g.
PATH=$PATH:$GOPATH/bin
The Glide package manager for Go can be found at it’s GitHub repository. Rather than follow the installation instructions contained in the GitHub repository, I encourage you to use the following steps.
-
Open a terminal/cmd window.
-
Type in
go get -u github.com/Masterminds/glide
and press enter. This will retrieve the dependency sources and Glide sources to the source directory, and build them into the pkg and bin directories inside of your $GOPATH. -
Type in
glide -v
and press enter. You should see output similar to the following:glide version dev
The IDEA Go plugin is compatible with a large set of JetBrains platform IDEs (e.g. IntelliJ IDEA, WebStorm, Android Studio, etc.). It can be found at it's GitHub page.
To install it in IDEA (Other JetBrains platforms may be similar, but no guarantees):
- Follow the instructions on the JetBrains site to add the plugin. When you are adding the repository in step 4 of the JetBrains instructions, use the value
https://plugins.jetbrains.com/plugins/alpha/5047
for the weekly build of the plugin. - Open the Preferences dialog.
- Select the Plugins settings.
- Type
go
in the Plugins search field. - Select the Go plugin and install it.
For those of you who are not using a JetBrains product, there are several other Go IDEs to choose from.
- Atom with the go-plus plugin
- Sublime with the GoSublime plugin
- The cross-platform LiteIDE X
- go-vim in docker to ease the pain of setting up and configuring go-vim and friends.
- Others can be found here
From within IntelliJ:
- Select File->Open.
- Navigate to the $GOPATH/src/02-hello directory.
- Select OK.
- Open hello.go in the project view.
- Click on the ‘Setup SDK’ link.
- Select your Go SDK. If it’s not available select Configure and perform the following steps:
- Click on + and select ‘Go SDK’.
- Navigate to your installation of the Go SDK. Most of the time, IntelliJ will have automatically selected the correct directory for you.
- Select OK in the file selection dialog.
- Select OK in the ‘Configure SDK’ dialog.
- Select OK in the ‘Select Project SDK’ dialog.
- Click on the ‘Change module type to Go and reload project’ link.
- Select ‘Reload Project’.
Next: Hello, Gopher!