Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

GO
Go
0/12 completed
Beginner
Hello GoTypes and variablesControl flowFunctions
Intermediate
Arrays, slices and mapsStructs and methodsInterfacesError handling
Advanced
GenericsGoroutines and channels
Pro
Building an HTTP serverModules, testing and tooling
Tracks / Go / Beginner

Hello Go

Go (often called Golang) was created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson, one of the inventors of Unix and C. They wanted a language as fast as C++ that was as easy to read as Python, compiled in seconds, and made concurrency simple. Today Go runs a huge part of the cloud: Docker, Kubernetes, Terraform, Prometheus, Grafana, etcd, Caddy and the backends of Uber, Twitch, Cloudflare and Dropbox are written in it.

💡 About the examples in this track: Go can't run inside a web page here, so every example shows its Output underneath. Every program was run with Go 1.27 to produce that output exactly. Copy any example and run it with go run main.go, or paste it into the official playground at go.dev/play.

Your first program

main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}
bash
go run main.go
Output
Hello, World!
Part Meaning
package main Every Go file belongs to a package. main is special: it builds an executable program
import "fmt" Use the standard library's formatting package
func main() The program starts here
fmt.Println(…) Print values followed by a newline. Names starting with a capital letter are exported (public)

No semicolons (the compiler inserts them), and the opening { must be on the same line as func. Go code is indented with tabs.

Compiling to a single binary

go run compiles to a temporary file and runs it. go build produces a real executable:

bash
go build -o hello main.go
./hello
ls -lh hello
file hello
Output
Hello, World!
2.3M
hello: ELF 64-bit LSB executable, ARM aarch64

The binary is statically linked: it contains everything it needs, including the Go runtime. Copy that one file to any Linux server and it runs, with no interpreter or libraries to install. That's a big reason Go is popular for command-line tools and containers (a Go app's Docker image can be a few megabytes).

Cross-compiling is just two environment variables:

bash
GOOS=windows GOARCH=amd64 go build -o hello.exe main.go
GOOS=darwin GOARCH=arm64 go build -o hello-mac main.go
file hello.exe hello-mac
Output
hello.exe: PE32+ executable for MS Windows 10.00 (console)
hello-mac: Mach-O 64-bit arm64 executable

Printing with fmt

go
package main

import "fmt"

func main() {
	name := "Asha"
	age := 31
	height := 1.68

	fmt.Println("Name:", name, "Age:", age)
	fmt.Printf("%s is %d years old and %.2f m tall\n", name, age, height)
	fmt.Printf("%v | %q | %T | %5d | %-6s|\n", age, name, height, 42, "go")
	greeting := fmt.Sprintf("Hi %s!", name)
	fmt.Println(greeting, len(greeting))
}
Output
Name: Asha Age: 31
Asha is 31 years old and 1.68 m tall
31 | "Asha" | float64 |    42 | go    |
Hi Asha! 8
Verb Prints
%v Any value in its default format (the most used)
%+v / %#v Structs with field names / as Go syntax
%d, %f, %.2f Integers, floats
%s, %q Strings, quoted strings
%T The value's type
%5d, %-6s Width: right-aligned, left-aligned

Println adds spaces between values and a newline; Printf formats; Sprintf returns the formatted string instead of printing it.

The compiler is strict, on purpose

Go refuses to compile code with unused variables or imports. It keeps programs clean, especially after refactoring:

go
package main

import (
	"fmt"
	"os"
)

func main() {
	count := 10
	fmt.Println("hello")
}
Output
# command-line-arguments
./main.go:5:2: "os" imported and not used
./main.go:9:2: declared and not used: count

Errors are precise and short. Most editors fix imports for you automatically when you save (using goimports, part of the Go extension).

Formatting: gofmt

Go has one official code style, applied by a tool. Nobody argues about tabs or brace positions:

messy.go
package main
import "fmt"
func main(){
x:=1;if x>0{fmt.Println( "positive" )}
}
bash
gofmt messy.go
Output
package main

import "fmt"

func main() {
	x := 1
	if x > 0 {
		fmt.Println("positive")
	}
}

gofmt -w file.go (or go fmt ./... for a whole project) rewrites the files in place. Editors run it on save, so all Go code in the world looks the same, which makes reading other people's code much easier.

Installing Go

  1. Download the installer from go.dev/dl (Windows, macOS, Linux), or:
    • macOS: brew install go
    • Linux: unpack the official archive into /usr/local/go and add /usr/local/go/bin to your PATH. (Distribution packages are often several versions behind.)
  2. Check: go version.
  3. Editor: VS Code with the official Go extension (it installs gopls, the language server, for autocomplete and errors), or GoLand.
bash
go version
Output
go version go1.27.4

Go at a glance

Go
Typing Static, with type inference (x := 5)
Memory Garbage collected: no malloc/free
Compiles to One native binary per OS/CPU
Concurrency Goroutines and channels, built into the language
Object model Structs, methods and interfaces; no classes or inheritance
Errors Returned as values, not exceptions
Standard library Very complete: HTTP servers, JSON, crypto, testing, templates

Go deliberately leaves features out (no inheritance, no exceptions, few ways to do each thing) so that code stays simple and readable by everyone on a team. You'll see that philosophy throughout this track.

Practice

  1. Print your name, age and city on one line with Printf, using %s, %d and %q.
  2. Print the type of 42, 4.2, "42" and true with %T.
  3. Build your program for Windows from Linux or macOS with GOOS=windows.
  4. Write some badly formatted code and fix it with gofmt.
✎ Check your understanding

1.What does go build produce?

2.What happens if you declare a variable and never use it?

3.Which function returns a formatted string instead of printing it?

4.What makes a name exported (public) in Go?