golang

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.

download golang from official website

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.

golang installer

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:

  1. Press the Windows key + R to open the “Run” dialog box.
  2. Type “sysdm.cpl” and press Enter.
  3. In the System Properties window, click the “Environment Variables” button.
  4. Under “System Variables”, scroll down and click on “New”.
  5. In the “Variable name” field, enter “GOPATH”.
  6. In the “Variable value” field, enter the path to your Go workspace directory (e.g. C:\Users\yourusername\go).
  7. Click “OK” to close the Environment Variables window.
  8. 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:

  1. Under “System Variables”, scroll down and find the “Path” variable.
  2. Click “Edit”.
  3. 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).
  4. 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.

golang version verification

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:

  1. package main: Every Go program must start with a package declaration. The main package is special because it defines a standalone executable program, rather than a library.
  2. import "fmt": This line imports the fmt package, which stands for “format”. The fmt package includes functions for formatting and printing text.
  3. func main(): This declares a function called main, which is the entry point for all Go programs. The main function is executed when you run the program.
  4. fmt.Println("Hello, World!"): This line calls the Println function from the fmt 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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *