Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Setting Up Go Environment

2. Setting Up Go Environment

Step 1: Download and Install Go:

  • Visit the official Go website: https://golang.org/dl
  • Download the installer appropriate for your operating system (Windows, macOS, Linux).
  • Run the installer and follow the installation instructions.

Step 2: Configure Go Paths:

After installing Go, you'll need to set up some environment variables to configure your Go workspace.

  • GOPATH: This is the root directory for your Go projects and source code. It is where Go will look for packages and where your compiled binaries will be placed.
    Set the GOPATH environment variable to your preferred directory. For example, you can add the following line to your shell profile ('.bashrc' or '.zshrc'):
export GOPATH=$HOME/go

 

  • GOBIN: This is the directory where Go binaries (executable files) will be placed after compilation.
    You can set the GOBIN environment variable to a directory within your GOPATH, like this:
export GOBIN=$GOPATH/bin

This ensures that your compiled binaries will be placed in the 'bin' directory within your GOPATH.

  • PATH: Add the 'bin' directory from your GOPATH to your PATH environment variable. This will allow you to execute Go binaries from the command line without specifying their full path.
export PATH=$PATH:$GOBIN

 

Step 3: Verify Installation:

  • Open a new terminal window or command prompt.
  • Run the following command to verify that Go is installed correctly:
go version

This should display the installed Go version, confirming that Go is properly installed and configured.

Step 4: Create Your Workspace:

  • Create a directory for your Go projects. You can name it anything you like. For example:
mkdir -p ~/go/src

 

  • Inside the 'src' directory, you can organize your projects within their respective import paths. For example, if you're working on a project called "myproject," create a directory structure like this:
~/go/src/myproject

 

Step 5: Test Your Setup:

  • Create a simple "Hello, World!" Go program. Create a file named 'main.go' inside your project directory ('myproject', for example):
package main

import "fmt"

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

 

  • Open a terminal and navigate to your project directory:
cd ~/go/src/myproject

 

  • Compile and run your program using the 'go run' command:
go run main.go

You should see the output "Hello, World!" printed to the terminal.

Congratulations! You've successfully set up your Go environment and created your first Go program. You're now ready to explore the language further and start building more complex applications.