Learn › Go Programming

Introduction to Go

Learn why Go was created, how to set up your development environment, and write your first Go program.

Why Go?

Go was created at Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson to address the challenges of building large-scale, concurrent software systems. It combines the performance of compiled languages like C with the simplicity and readability of dynamically typed languages like Python. Go compiles to native machine code, produces statically linked binaries, and has a garbage collector that makes memory management straightforward. Its design philosophy emphasizes simplicity, readability, and pragmatism over clever abstractions.

Setting Up Your Environment

To get started with Go, download the installer from the official Go website at go.dev and follow the instructions for your operating system. After installation, verify it works by running 'go version' in your terminal. Go uses a workspace model where your code lives inside modules, and the GOPATH environment variable historically pointed to your workspace directory. Modern Go development uses Go modules, which allow you to place projects anywhere on your filesystem.

# Verify installation
go version

# Initialize a new module
mkdir myproject && cd myproject
go mod init github.com/username/myproject

Hello World

Every Go program starts with a package declaration, and the entry point of an executable program must be in the 'main' package. The 'fmt' package from the standard library provides formatted I/O functions similar to C's printf. Go enforces strict rules about unused imports and variables, which helps keep codebases clean. The main function takes no arguments and returns no values — command-line arguments are accessed through the os package.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
    fmt.Printf("Go is %s!\n", "awesome")
}

Running and Building

Go provides two main commands for executing your code: 'go run' and 'go build'. The 'go run' command compiles and runs the program in one step, which is useful during development. The 'go build' command compiles the program and produces a standalone binary that you can distribute and run without needing Go installed. Go's fast compilation times are one of its most praised features, making the edit-compile-run cycle nearly instantaneous even for large projects.

# Run without producing a binary
go run main.go

# Build a binary
go build -o myapp main.go

# Run the binary
./myapp

# Install the binary to $GOPATH/bin
go install

Variables & Types →