Welcome to the world of Go! In this tutorial, we’ll be covering how to install and set up Go on your local machine. Whether you’re a seasoned software developer or just starting out, this guide will help you get started with Go and start building your own projects.
Installation
First, you’ll need to download the Go installer from the official Go website. There are versions available for Windows, macOS, and Linux.
Once the download is complete, run the installer and follow the prompts to install Go. Since I have MacOS I run an installer for Apple macOS, but for Windows it’s would be the same.
On Linux systems, you can also install Go using your package manager. For example, on Debian or Ubuntu systems, you can use apt-get
:
sudo apt-get update
sudo apt-get install golang
Setup
After installing Go, you’ll need to set up your Go workspace. This is the directory where your Go code and dependencies will be stored. By default, the Go workspace is located at ~/go
on Unix-based systems and %USERPROFILE%\go
on Windows.
Windows
On Windows, you can create the Go workspace directory by opening a command prompt and running the following command:
mkdir %USERPROFILE%\go\src
Next, you’ll need to set the GOPATH
environment variable to point to your Go workspace directory. To do this, follow these steps:
- Press the Windows key + R to open the “Run” dialog box.
- Type “sysdm.cpl” and press Enter.
- In the System Properties window, click the “Environment Variables” button.
- Under “System Variables”, scroll down and click on “New”.
- In the “Variable name” field, enter “GOPATH”.
- In the “Variable value” field, enter the path to your Go workspace directory (e.g.
C:\Users\yourusername\go
). - Click “OK” to close the Environment Variables window.
- Click “OK” again to close the System Properties window.
You’ll also need to add the bin
directory inside your Go workspace to your PATH
variable. To do this, follow these steps:
- Under “System Variables”, scroll down and find the “Path” variable.
- Click “Edit”.
- In the “Edit environment variable” window, click “New” and add the path to the
bin
directory inside your Go workspace (e.g.C:\Users\yourusername\go\bin
). - Click “OK” to close the window.
Unix-based systems (including macOS)
On Unix-based systems, you can create the Go workspace directory by running the following command:
mkdir -p $HOME/go/src
Next, you’ll need to set the GOPATH
environment variable to point to your Go workspace directory. You can do this by adding the following line to your ~/.bashrc
or ~/.bash_profile
file:
export GOPATH=$HOME/go
You’ll also need to add the bin
directory inside your Go workspace to your PATH
variable:
export PATH=$PATH:$GOPATH/bin
Note: that the GOPATH environment variable on Unix-based systems can also be set in the ~/.bash_profile file, depending on the user’s configuration. This file is sourced by the Bash shell for login shells, and can be used to set environment variables and run commands when a user logs in. If you prefer to use the ~/.bash_profile file instead of the ~/.bashrc file to set the GOPATH environment variable, simply add the following line to the file:
export GOPATH=$HOME/go
This will ensure that the GOPATH is set correctly when you log in to your Unix-based system.
Verifying the installation
Finally, you can verify that Go is properly installed by running the following command:
go version
This should output the version of Go that you have installed.
Hello, World!
Now that you have Go set up on your machine, let’s write your first Go program. Open your favorite IDE, personally, I recommend Goland, you can learn more about this powerful IDE in another article. Create a new file called hello.go
and add the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This is a very simple Go program that prints “Hello, World!” to the console. Let’s go through the code line by line:
package main
: Every Go program must start with a package declaration. Themain
package is special because it defines a standalone executable program, rather than a library.import "fmt"
: This line imports thefmt
package, which stands for “format”. Thefmt
package includes functions for formatting and printing text.func main()
: This declares a function calledmain
, which is the entry point for all Go programs. Themain
function is executed when you run the program.fmt.Println("Hello, World!")
: This line calls thePrintln
function from thefmt
package to print the string “Hello, World!” to the console.
To run the program, open a terminal window and navigate to the directory where hello.go
is located. Then, run the following command:
go run hello.go
You should see the output “Hello, World!” printed on the console.
Congratulations! You have successfully installed and set up Go on your machine. In the next tutorial, we’ll cover the basics of Go syntax and data types.
Resources
- The Go Programming Language: The official website for Go, with links to documentation, downloads, and more.
- The Go Tour: An interactive tutorial that introduces you to the basics of Go.
- Effective Go: A guide to writing clear, idiomatic Go code.
- Go by Example: A collection of short Go programs that demonstrate how to use various language features.
- GoDoc: A searchable database of Go packages and their documentation.
- Go Forum: A community forum for Go programmers.
- Golang Slack: A Slack community for Go developers.
- GopherAcademy: A learning resource for Go, with articles, tutorials, and conference videos.
- Go Weekly: A weekly newsletter with the latest Go news and articles.
- Gophercon: The annual conference for the Go community.