Go, also referred to as golang, is a modern, open-source programming language developed by Google. Increasingly popular for many applications, Go takes a minimalist approach to development, helping you build reliable and efficient software.
This tutorial will guide you through downloading and installing Go, as well as compiling and executing a basic “Hello, World!” program, on a Debian 8 server.
This tutorial assumes you have access to a Debian 8 server, configured with a non-root user with sudo
privileges as described in Initial Server Setup with Debian 8.
In this step, we’ll install Go on your server.
Visit the official Go downloads page and find the URL for the current binary release’s tarball. Make sure you copy the link for the latest version that is compatible with a 64-bit architecture.
From your home directory, use curl
to retrieve the tarball:
- curl -O https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz
Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the Internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum
command produces a unique 256-bit hash:
- sha256sum go1.10*.tar.gz
Output4b677d698c65370afa33757b6954ade60347aaca310ea92a63ed717d7cb0c2ff
Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.
With Go downloaded and the integrity of the file validated, let’s proceed with the installation.
We’ll use tar
to extract the tarball. The x
flag tells tar
to extract, v
tells it we want verbose output (a listing of the files being extracted), and f
tells it we’ll specify a filename:
- tar xvf go1.10.2.linux-amd64.tar.gz
You should now have a directory called go
in your home directory. Recursively change go
’s owner and group to root, and move it to /usr/local
:
- sudo chown -R root:root ./go
- sudo mv go /usr/local
Note: Although /usr/local/go
is the officially-recommended location, some users may prefer or require different paths.
At this point, using Go would require specifying the full path to its install location in the command line. To make interacting with Go more user-friendly, we will set a few paths.
In this step, we’ll set some paths in your environment.
First, set Go’s root value, which tells Go where to look for its files.
- nano ~/.profile
At the end of the file, add this line:
...
export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
If you chose an alternate installation location for Go, add these lines instead to the same file. This example shows the commands if Go is installed in your home directory:
...
export GOROOT=$HOME/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
With the appropriate line pasted into your profile, save and close the file. Next, refresh your profile by running:
- source ~/.profile
With the core of Go in place, let’s confirm that our setup works by composing a short program.
Now that Go is installed and the paths are set for your server, you can test to ensure that Go is working as expected.
Create a new directory for your Go workspace, which is where Go will build its files:
- mkdir $HOME/work
Then, create a directory hierarchy in this folder in order for you to create your test program file. We’ll use the directory my_project
as an example.
- mkdir -p work/src/my_project/hello
Next, you can create a traditional “Hello World” Go file.
- nano ~/work/src/my_project/hello/hello.go
Inside your editor, paste the code below, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print “Hello, World” when run.
package main
import "fmt"
func main() {
fmt.Printf("Hello, World!\n")
}
This program will print “Hello, World!” if it successfully runs, which will indicate that Go programs are compiling correctly. Save and close the file, then compile it by invoking the Go command install
:
- go install my_project/hello
With the program compiled, you can run it by executing the command:
- hello
Go is successfully installed and functional if you see the following output:
OutputHello, World!
You can see where the compiled hello
binary is installed by using the which
command:
- which hello
Output/home/user/work/bin/hello
The “Hello, World!” program established that you have a Go development environment.
By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. A typical application will use libraries and remote packages. To learn more about working with these additional components, check out the official documentation on How to Write Go Code.
You can also read some Go tips from our development team.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Important Note: you must use flag -L when curl’ing. If you do not, you’re not actually getting any data (due to redirects) and the checksum will not match.
If you do: curl -O -L <url_of_current_version>
…then once you sha256sum the file, it will match Google’s DL page.
I think you left out the
z
flag totar
?Hi Great article. One question: why would one edit root’s .profile? I’m not sure if this is misworded, since if I’m logged in as a user ‘normaluser’ and run the command: ‘sudo nano ~/.profile’, then it will edit normaluser’s .profile. Really we don’t need to run this as sudo.
Have I misunderstood?