Go Installation and Setup
Go (Golang) is a statically typed, compiled programming language designed for simplicity, efficiency, and concurrency. This tutorial covers installing Go, setting up your development environment, and configuring your workspace with modules.
1. Installing Go
Go installation is straightforward across all major operating systems. The official binaries are available from the Go website and include everything needed to start developing.
Windows Installation
# Download the MSI installer from:
# https://golang.org/dl/
# Run the installer (default settings are recommended)
# Adds Go to your system PATH automatically
# Verify installation
go version
macOS/Linux Installation
# Download the package from:
# https://golang.org/dl/
# macOS (using Homebrew):
brew install go
# Linux (Debian/Ubuntu):
sudo apt-get update
sudo apt-get install golang
# Verify installation
go version
Key Points: The installer sets up Go binaries in /usr/local/go (Unix) or c:\Go (Windows). Installs the complete toolchain (compiler, formatter, dependency manager). Current version as of 2023 is 1.20+.
Installation Quiz
What command verifies Go is installed correctly?
2. Configuring Your Environment
Go environment variables control where Go looks for code, dependencies, and how it builds programs. The most important are GOPATH and GOBIN.
# View current Go environment
go env
# Common variables to set (add to your shell config):
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
# For Go Modules (modern approach):
export GO111MODULE=on
# Set proxy for China users:
export GOPROXY=https://goproxy.cn,direct
Environment Variables: GOPATH was traditionally required but is now optional with modules. GOBIN controls where installed binaries go. GO111MODULE enables the module system.
Environment Quiz
What does GOPATH control in Go?
3. Creating a Workspace
Go workspaces organize your projects and dependencies. Modern Go uses modules (go.mod) rather than the traditional GOPATH workspace.
# Create a new project directory
mkdir myproject && cd myproject
# Initialize a new module (creates go.mod)
go mod init github.com/yourname/myproject
# Typical project structure:
myproject/
├── go.mod # Module definition
├── go.sum # Dependency checksums
├── main.go # Entry point
└── internal/ # Private code
└── ...
// Sample main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Module Benefits: No need to work in GOPATH. Explicit dependency tracking. Versioned dependencies. Better reproducibility.
Workspace Quiz
What command initializes a new Go module?
4. Managing Dependencies
Go modules automatically handle dependency management. The go.mod file tracks requirements and versions.
# Add a dependency (will update go.mod)
go get github.com/gin-gonic/[email protected]
# Download all module dependencies
go mod download
# Tidy up unused dependencies
go mod tidy
# Vendor dependencies (optional)
go mod vendor
// Using the imported package
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "hello"})
})
r.Run()
}
Dependency Tips: Always specify versions with @. Use go mod tidy regularly. Check in go.mod and go.sum to version control.
Dependency Quiz
What does go mod tidy do?
5. Essential Development Tools
Go toolchain includes powerful built-in tools for formatting, testing, and analyzing code.
# Format your code (enforces standard style)
go fmt ./...
# Run tests
go test ./...
# Build and install
go build
go install
# Static analysis
go vet ./...
# View documentation
go doc fmt.Println
# Popular third-party tools:
# Linter (golangci-lint):
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.51.2
# Code completion (gopls):
go install golang.org/x/tools/gopls@latest
Tool Benefits: go fmt ensures consistent style. go vet catches common errors. gopls enables IDE features. golangci-lint provides comprehensive linting.
Tools Quiz
What does go fmt do?
6. IDE and Editor Configuration
Editor support for Go is excellent across modern IDEs. Proper setup enhances productivity with features like autocompletion and debugging.
Visual Studio Code
# Install the Go extension:
# Search for "Go" in VSCode extensions
# Recommended settings:
{
"go.useLanguageServer": true,
"go.formatTool": "goimports",
"go.lintTool": "golangci-lint",
"go.testFlags": ["-v"]
}
Goland (JetBrains)
# Full-featured commercial IDE
# Includes everything preconfigured:
# - Debugging
# - Refactoring tools
# - Integrated testing
# - Database tools
IDE Features: Code completion. Integrated debugging. Test runners. Refactoring tools. Documentation on hover. Most IDEs require gopls for best results.
IDE Quiz
What language server is recommended for Go IDE support?
7. Writing Your First Program
Hello World in Go demonstrates the language's simplicity and conventions.
package main // Executable packages use "main"
import "fmt" // Formatting package from stdlib
func main() {
// Print to stdout with newline
fmt.Println("Hello, World!")
// Variables with type inference
message := "Hello, Go!"
fmt.Println(message)
}
# Run the program
go run main.go
# Build executable
go build -o hello
# Install to $GOBIN
go install
Go Basics: Packages organize code. main is the entry point. := declares and initializes. Exported names are capitalized. Strong standard library.