Skip to content

Go Environment Setup

To start programming in Go, you need to set up a development environment on your computer. This typically involves installing the Go distribution (compiler, tools, and standard library) and choosing a text editor or Integrated Development Environment (IDE).

If you want to experiment with Go code quickly without installing anything locally, you can use the Go Playground. It’s an online tool that lets you write, run, and share Go code directly in your web browser. Most simple examples in this tutorial can be tested there.

  1. Download Go Distribution: Visit the official Go website at go.dev/dl/. Download the installer or archive file appropriate for your operating system (Windows, macOS, Linux) and architecture (e.g., amd64, arm64).
  2. Install Go:
    • Windows: Run the MSI installer and follow the prompts. It will typically install Go to C:\Program Files\Go or C:\Go and attempt to add C:\Go\bin to your system PATH.
    • macOS: Use the PKG installer, or if you use Homebrew: brew install go.
    • Linux: Extract the downloaded archive (e.g., tar -C /usr/local -xzf goX.Y.Z.linux-amd64.tar.gz). Then, add /usr/local/go/bin to your shell’s PATH environment variable (e.g., in ~/.bashrc, ~/.zshrc, or /etc/profile): export PATH=$PATH:/usr/local/go/bin After installation or updating your PATH, open a new terminal window for the changes to take effect.
  3. Verify Installation: Open a new terminal or command prompt and type: go version You should see output like go version goX.Y.Z os/arch (e.g., go version go1.21.0 linux/amd64).
  4. Choose a Text Editor or IDE: You can write Go code in any text editor, but using one with Go support enhances productivity. Popular choices include:
    • Visual Studio Code (VS Code): Excellent Go support via the official Go extension (golang.Go). Offers features like IntelliSense, debugging, testing, and linting.
    • GoLand: A powerful commercial IDE from JetBrains, specifically designed for Go development.
    • Vim, Emacs, Sublime Text: With appropriate Go plugins, these editors also provide a good Go development experience.
  5. Understanding Go Workspaces and Modules:
    • GOPATH (Legacy): Historically, Go used a GOPATH environment variable to define a workspace for all Go projects and their dependencies. While still supported, this model is largely superseded by Go Modules.
    • Go Modules (Current Standard): Introduced in Go 1.11 and enabled by default since Go 1.13, Go Modules are the standard way to manage project dependencies. They allow projects to reside outside the GOPATH and define their own dependencies in a go.mod file.

Setting Up Your First Go Project (with Modules)

Section titled “Setting Up Your First Go Project (with Modules)”
  1. Create a new directory for your project anywhere on your filesystem (it doesn’t need to be in GOPATH): mkdir mygoproject cd mygoproject
  2. Initialize a Go module. This creates a go.mod file to track dependencies. The module path is typically a URL to your repository (e.g., github.com/yourusername/mygoproject) or just a project name for local projects: go mod init mygoproject.example/hello (or go mod init mygoproject for simpler local projects)
  3. Create your first Go file, e.g., main.go:
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello from my Go module!")
}
  1. Run your program from within the project directory:
go run main.go

You should see the output: Hello from my Go module!

  • go run <filename.go>: Compiles and runs a Go program without producing a separate executable binary.
  • go build: Compiles the packages and dependencies in the current directory, producing an executable binary (if it’s a main package).
  • go install: Compiles and installs packages or commands (typically placing binaries in $GOPATH/bin or $GOBIN).
  • go test: Runs tests for the current package.
  • go get <package_path>: Adds new dependencies to your current module (modifies go.mod and go.sum).
  • go fmt <path>: Formats Go source code according to Go conventions.
  • go mod tidy: Prunes unused dependencies and adds required ones to go.mod and go.sum.

With these steps, your Go development environment is ready for you to start building applications!