Welcome to go-tutorial.com, a free Go tutorial that starts from zero. On this page you’ll meet your first Go program, understand every line of it, and see what happens when you get something wrong. If you already know another language, skim the first sections and jump to the code.
Can I use this Go tutorial as a complete beginner?
Yes. Most Go tutorials, courses and books assume you already know a programming language. I don’t. I do assume you’re comfortable with a computer: you can install software, find a file you saved, and open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux).
This isn’t a computer science course, so now and then you may want to look something up. Where an outside page explains a topic better than I can in a paragraph, I’ll link to it.
So what is Go?
Go (often called Golang, after its old website golang.org) is an open-source programming language that was created at Google and first released in 2009. It’s a compiled language: a tool called the compiler turns your Go code into a program your operating system can run directly, without Go installed. If you’ve seen other languages, Go’s syntax looks a bit like C, but it feels much smaller and simpler.
Unlike C, Go manages memory for you with a garbage collector, which removes a whole class of hard-to-find bugs. It also has concurrency built into the language: goroutines let a program do many things at once, and channels let those goroutines talk to each other safely.
Go is a general-purpose language, but it really shines for servers and web backends, command-line tools, and cloud infrastructure. Docker and Kubernetes, for example, are written in Go.
Why learn Go?
Whether these matter to you is your call, but here’s why I like it:
- It’s small. The language has few keywords and usually one obvious way to do something, so code written by others is easy to read.
- It’s statically typed. The compiler catches many mistakes before your program ever runs, and you’ll see that in action below.
- It builds a single, self-contained program. Copy one file to another machine and run it. Building for another operating system takes one command.
- Concurrency is part of the language, not an add-on library.
- The standard library and tools come in the box: an HTTP server, JSON, testing, formatting (
go fmt) and more, without installing anything extra. - Old code keeps working. The Go 1 compatibility promise means programs written for Go 1.0 in 2012 should still build with today’s Go.
Does it run everywhere?
Pretty much. Go runs on Windows, macOS and Linux, plus FreeBSD, OpenBSD, NetBSD and a few more, on Intel/AMD and ARM processors. It can even compile to WebAssembly for the browser. The same Go code usually builds on all of them without changes.
How this tutorial works
The lessons follow a learning path in six stages, from getting started to building real programs. Each lesson builds on the ones before it, so if you’re new to programming, read them in order. Every complete example is followed by the exact output it prints, and most lessons end with a small exercise.
To run the examples on your own computer, you need Go installed. If you haven’t done that yet, follow Install Go first. It takes about ten minutes. You can also paste any example into the Go Playground and run it in your browser.
Hello, world!
Tradition says your first program in a new language prints a greeting. Here it is in Go. Have a good look at it first, then I’ll go through it line by line.
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}Code language: Go (go)
When you run it, it prints:
Hello, 世界Code language: plaintext (plaintext)
If you’ve programmed before, this probably looks familiar. If you haven’t, it may look like a lot of strange words for printing one line. Don’t worry: there are only five things going on.
The file
Go code lives in plain text files that end in .go. I’ll call this one hello.go. You can create it with any editor; Install Go shows how to set up VS Code for it.
Line 1: package main
Every Go file starts by saying which package it belongs to. A package is a group of Go files that belong together. The name main is special: it tells Go that this package is a program you can run, rather than a library for other code to use. You’ll learn much more about packages in Packages and modules.
Line 3: import “fmt”
An import makes another package’s code available in this file. fmt (say “fumpt” or “format”, people use both) is part of Go’s standard library and contains functions for formatting and printing text. Imports always come right after the package line. To import more than one package, you put them in parentheses:
import (
"fmt"
"os"
)Code language: Go (go)
Go is strict about imports: if you import a package and don’t use it, the program won’t compile. That keeps files free of leftovers.
Line 5: func main() {
A function is a named block of code. You define one with the keyword func, a name, and a pair of parentheses. The parentheses can hold input values for the function (you’ll learn about those in Functions); main takes none. The code of the function goes between the curly braces { and }.
The function main in package main is where every Go program starts. When you run the program, Go calls main, runs its lines from top to bottom, and the program ends when main ends. Leave it out (or call it Main) and Go refuses to build the program with the message function main is undeclared in the main package.
Line 6: fmt.Println(“Hello, 世界”)
This line does the actual work. It calls the function Println (“print line”) from the fmt package. Because Println lives in another package, you write the package name, a dot, then the function name.
Between the parentheses is the value we give it: "Hello, 世界". Text between double quotes is called a string. Println prints it and then moves to a new line. You can give it several values, separated by commas, and it prints them with a space in between.
世界 means “world” in Chinese and Japanese. I put it there to show that Go handles Unicode out of the box: Go source files are UTF-8, so you can put text in any language (and emoji) in your strings.
Line 7: the closing brace
The } ends the main function, and with it the program. Notice that there are no semicolons at the end of lines. Go adds them for you behind the scenes. A side effect of that rule is that an opening brace must be on the same line as func main(), not on the next line.
Formatting: let the tool decide
Go code is indented with tabs, and there’s one official layout for it. You don’t have to memorize it: the gofmt tool (also available as go fmt) rewrites your file into the standard format, and VS Code with the Go extension does that every time you save. The result is that all Go code looks alike, whoever wrote it, and nobody argues about where braces go.
Breaking it on purpose
You’ll make typos. Everybody does. The good news is that the Go compiler catches many of them and tells you exactly where they are. Let’s misspell Println as Prinln:
package main
import "fmt"
func main() {
fmt.Prinln("Hello, 世界")
}Code language: Go (go)
Running it with go run hello.go (more on that command in the next lesson):
go run hello.goCode language: Bash (bash)
# command-line-arguments
./hello.go:6:6: undefined: fmt.PrinlnCode language: plaintext (plaintext)
Here’s how to read it. The first line names what was being built (command-line-arguments is what Go calls a program built from files you listed by name). Then comes the file, the line number (6) and the column (6), and finally what’s wrong: there is no Prinln in package fmt. Nothing ran. The compiler stops before your program starts, which is much better than finding the mistake when a user hits it.
When you get a compile error, look at the first one first. Go to the line and column it names, fix it, and run again. Later errors are sometimes a side effect of the first.
Running it yourself
Save the working version as hello.go, open a terminal in that folder, and run go run hello.go. The next lesson, Build and run a Go program, explains what that command does, how to turn your code into a program you can share, and what a go.mod file is.
Not for you?
If after this introduction Go doesn’t feel like the right first language, I understand. Go is general-purpose, but it’s less forgiving for beginners than Python. Please don’t give up on programming because of that. I also write a beginner-friendly Python tutorial on Python Land, and many people start there and come back to Go later.
Practice
1. Change the program so it prints three lines: a greeting, your name, and your favorite food. Use one fmt.Println call per line.
Show a solution
package main
import "fmt"
func main() {
fmt.Println("Hello!")
fmt.Println("My name is Erik.")
fmt.Println("I like pizza.")
}Code language: Go (go)
Hello!
My name is Erik.
I like pizza.Code language: plaintext (plaintext)2. Print Go is fun using a single fmt.Println call with three separate strings. What does Println put between them?
Show a solution
package main
import "fmt"
func main() {
fmt.Println("Go", "is", "fun")
}Code language: Go (go)
Go is funCode language: plaintext (plaintext)Println puts a space between the values and a newline at the end.
Key takeaways
- Go is a small, compiled, statically typed language with garbage collection and built-in concurrency.
- A runnable program has
package mainand afunc main(), which is where it starts. importbrings in other packages, such asfmtfor printing. Unused imports are an error.- Compile errors name the file, line and column. Read them; they’re usually precise.
- Let
gofmt(or your editor) format your code.