AheadKit

Free chapter — read online

Go for Everyone: Chapter 1, free to read

This is a free chapter of Go for Everyone: Chapter 1 — “Introduction” — all 5 pages (pages 5–9 of the full 294-page book), exactly as they appear in the book. No edits, no teaser cuts.

Chapter 1: Introduction

Version: Go 1.27

Welcome to Go. Go is a programming language created at Google. It was first released in 2009, designed by Robert Griesemer, Rob Pike, and Ken Thompson. The three of them worked on a simple problem: the software Google ran on its servers was getting bigger, builds were getting slower, and the languages they used felt heavier than the job needed.

Their answer was a small language. Go has few keywords, one standard way to format code, and a compiler that finishes fast. It was designed in the era of cloud servers and many-core machines, so doing several things at once is built into the language instead of added later through libraries.

What Go Is Used For

Most Go programs run on servers, not on phone screens. When an app on your phone asks for data, a program on a server answers. A lot of those answering programs are written in Go.

Three common homes for Go programs:

Two facts show how far that went. Docker, the tool that packages programs into containers, is written in Go. Kubernetes, the system that runs those containers across many machines, is also written in Go. When the people building cloud tools picked a language, they picked this one.

Go is also popular for command-line tools: programs you run by typing a command, which do one job and print the result. If you can write a small tool, you can write the start of a server, because both use the same language and the same standard library.

Why Learn Go

Here are the honest reasons, in the order most beginners feel them:

  • It is small. You can learn the whole language. There is one loop, one main string type, and a short list of keywords. Most of what you write in month one still looks right in year three.
  • It compiles fast. Turning your code into a runnable program takes a second or two for small projects, so the write-run-fix loop stays tight.
  • It ships as one file. A built Go program is a single binary file. Copy that one file to a server and run it. There is nothing else to install.
  • Concurrency is built in. Go can start thousands of lightweight threads called goroutines with one keyword. Servers live on this, because a server answers many people at the same time. We build up to goroutines later in this book, one step at a time.
  • Companies hire for it. Backend and cloud job posts ask for Go by name, because so much server software already runs on it. NOTE Go is a compiled language. You write source code, a compiler turns it into machine code, and the machine runs that. If the compiler finds a mistake, the program does not run at all, and the error tells you which line to look at.

Setting Up

You need exactly one thing: the Go toolchain, which contains the compiler and the go command.

  • Go to go.dev and download the installer for your operating system.
  • Run the installer and accept the defaults.
  • Open a terminal (Terminal on macOS and Linux, PowerShell or Command Prompt on Windows).
  • Ask the toolchain for its version:
go version

Output:

go version go1.27.3 linux/amd64

That line came from the machine used to write this book: Go 1.27.3, on Linux, on a 64-bit Intel/AMD processor. Your line may end differently if you use Windows or a Mac. What matters is that you get a version line at all. If the terminal says the command was not found, the install did not finish or the terminal was open during the install. Close the terminal, open a fresh one, and try again.

GOTCHA One install is enough. You do not need a separate editor plugin, database, or web server to follow this book. A text editor and a terminal are the whole setup.

Your First Program

Create a new folder for the project, move into it, and start a Go module. A module is Go's way of saying "these files belong to one project":

mkdir hello
cd hello
go mod init example.com/hello

Now create a file named main.go in that folder and type this into it:

package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}

Run it:

go run .

Output:

Hello, world!

That is a complete Go program. It prints one line and ends. Five lines of code, no setup inside the file, nothing else needed.

Do not worry about what each line means yet. The next chapter takes this exact program apart, line by line. For now, notice the shape of the work: you write a .go file, you run a go command, something happens.

From Source Code to a Running Program

The go run command hides a step. Underneath, Go compiles your source code into a binary file, then runs that file. You can do the two steps separately with go build , and Chapter 2 does exactly that.

The journey of a Go program, from the file you write to the output you see:

The single-file part is the one working programmers care about. The binary built on the machine that wrote this book was 2,342,377 bytes, about 2.3 MB, and it contains everything the program needs. A server does not need Go installed to run it. It just needs the file.

Try It Yourself

  • Change the first program so it prints your name on one line and your city on the next line. Use two
fmt.Println calls.

Answer

package main
import "fmt"
func main() {
fmt.Println("My name is Sam.")
fmt.Println("I live in Dhaka.")
}

Output:

My name is Sam.
I live in Dhaka.

Use your own name and city in place of Sam and Dhaka.

  • Write a program that prints three separate lines, one line for each reason you want to learn Go. Use one fmt.Println per line.

Answer

package main
import "fmt"
func main() {
fmt.Println("Go is simple to read.")
fmt.Println("Go compiles fast.")
fmt.Println("Go programs ship as one file.")
}

Output:

Go is simple to read.
Go compiles fast.
Go programs ship as one file.

Your three reasons can say anything. The skill being practiced is one print statement per line.

  • fmt.Println can take several values at once, and it puts a space between them. Write a program that prints Go is fun using one fmt.Println call with three values, then prints the line Ready to code in Go! twice.

Answer

package main
import "fmt"
func main() {
fmt.Println("Go", "is", "fun")
fmt.Println("Ready to code in Go!")
fmt.Println("Ready to code in Go!")
}

Output:

Go is fun
Ready to code in Go!
Ready to code in Go!

Notice the spaces in `Go is fun`. You did not type them. `Println` added a space between each pair of values, and the `ln` at the end of its name means it moves to a new line after printing. You have a working program, but right now it is five lines of magic. In the next chapter we slow down and read those five lines properly: what a package is, why the program starts at main, and what go run and go build each do with your file.

That was Chapter 1. The book has 23 more chapters.

The full book: 294 pages, 24 chapters and 10 step-by-step projects, for $2.99 — or all 11 programming books in the bundle for $12.99.

Added to cart