Skip to main content
โšก Calmops

Go Installation and Environment Setup

Go Installation and Environment Setup

Setting up Go correctly is the first step to productive development. This guide covers installation across different operating systems and proper environment configuration.

Installing Go

macOS Installation

# Using Homebrew (recommended)
brew install go

# Verify installation
go version

# Check installation location
which go

Linux Installation

# Download latest Go version
wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz

# Extract to /usr/local
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz

# Add to PATH in ~/.bashrc or ~/.zshrc
export PATH=$PATH:/usr/local/go/bin

# Reload shell
source ~/.bashrc

# Verify installation
go version

Windows Installation

# Download MSI installer from https://go.dev/dl/
# Run the installer and follow prompts

# Verify installation (in PowerShell)
go version

# Check installation location
where go

Environment Variables

Essential Go Environment Variables

# GOROOT: Go installation directory
export GOROOT=/usr/local/go

# GOPATH: Go workspace directory
export GOPATH=$HOME/go

# Add Go binaries to PATH
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# Verify settings
go env

Checking Environment

# View all Go environment variables
go env

# View specific variable
go env GOPATH
go env GOROOT
go env GOOS
go env GOARCH

Go Workspace Structure

Traditional GOPATH Workspace (Pre-Go 1.11)

$GOPATH/
โ”œโ”€โ”€ bin/          # Compiled binaries
โ”œโ”€โ”€ pkg/          # Compiled packages
โ””โ”€โ”€ src/          # Source code
    โ”œโ”€โ”€ github.com/
    โ”‚   โ””โ”€โ”€ username/
    โ”‚       โ””โ”€โ”€ project/
    โ””โ”€โ”€ example.com/
        โ””โ”€โ”€ package/

Modern Go Modules (Go 1.11+)

my-project/
โ”œโ”€โ”€ go.mod        # Module definition
โ”œโ”€โ”€ go.sum        # Dependency checksums
โ”œโ”€โ”€ main.go       # Main package
โ”œโ”€โ”€ cmd/
โ”‚   โ””โ”€โ”€ myapp/
โ”‚       โ””โ”€โ”€ main.go
โ”œโ”€โ”€ internal/
โ”‚   โ””โ”€โ”€ mypackage/
โ”‚       โ””โ”€โ”€ package.go
โ”œโ”€โ”€ pkg/
โ”‚   โ””โ”€โ”€ publicpackage/
โ”‚       โ””โ”€โ”€ package.go
โ””โ”€โ”€ tests/
    โ””โ”€โ”€ main_test.go

Setting Up Your First Project

Creating a New Module

# Create project directory
mkdir my-go-app
cd my-go-app

# Initialize Go module
go mod init github.com/username/my-go-app

# Create main.go
cat > main.go << 'EOF'
package main

import "fmt"

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

# Run the program
go run main.go

# Build executable
go build

# Run executable
./my-go-app

go.mod File

module github.com/username/my-go-app

go 1.21

require (
    github.com/some/package v1.2.3
)

Verifying Installation

Complete Setup Check

#!/bin/bash

echo "=== Go Installation Check ==="

# Check Go version
echo "Go version:"
go version

# Check GOROOT
echo -e "\nGOROOT:"
go env GOROOT

# Check GOPATH
echo -e "\nGOPATH:"
go env GOPATH

# Check OS and Architecture
echo -e "\nOS and Architecture:"
echo "GOOS: $(go env GOOS)"
echo "GOARCH: $(go env GOARCH)"

# Check Go binary location
echo -e "\nGo binary location:"
which go

# Test compilation
echo -e "\nTesting compilation:"
go version

echo -e "\nโœ“ Go installation verified!"

Troubleshooting

Common Issues

# Issue: "go: command not found"
# Solution: Add Go to PATH
export PATH=$PATH:/usr/local/go/bin

# Issue: GOPATH not set
# Solution: Set GOPATH explicitly
export GOPATH=$HOME/go

# Issue: Module not found
# Solution: Initialize module
go mod init github.com/username/project

# Issue: Dependency conflicts
# Solution: Clean and download dependencies
go clean -modcache
go mod download

Verifying PATH

# Check if Go is in PATH
echo $PATH

# Verify Go binary is accessible
go version

# Check Go binary location
which go

IDE and Editor Setup

VS Code Setup

# Install Go extension
# Open VS Code and search for "Go" in extensions
# Install the official Go extension by Google

# Install Go tools
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/go-delve/delve/cmd/dlv@latest

GoLand Setup

# Download from https://www.jetbrains.com/go/
# Install and configure Go SDK path
# GoLand will auto-detect Go installation

Best Practices

โœ… Good: Proper Environment Setup

# Set up environment variables in shell profile
cat >> ~/.bashrc << 'EOF'
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOF

source ~/.bashrc

โŒ Bad: Incorrect PATH Configuration

# DON'T: Forget to add Go to PATH
# This will result in "go: command not found"

Summary

Proper Go installation and environment setup ensures:

  1. Correct Go version installed
  2. GOPATH and GOROOT properly configured
  3. PATH includes Go binaries
  4. IDE integration working correctly
  5. Module system ready for projects

With these foundations in place, you’re ready to start Go development!

Comments