Skip to content

01 - Setup

What this session is

About 30 minutes. By the end you'll have Go installed, a place to put your code, and one tiny program that prints "hello, world" - the traditional first thing.

Step 1: Install Go

Go to go.dev/dl. Download the installer for your operating system.

  • macOS: download the .pkg file. Double-click; follow the prompts.
  • Windows: download the .msi file. Double-click; follow the prompts.
  • Linux: either download the .tar.gz from go.dev/dl and follow the instructions there, or use your distro's package manager: sudo apt install golang-go on Debian/Ubuntu, sudo dnf install golang on Fedora.

Once it's done, open a terminal.

  • macOS: press ⌘ Space, type "terminal", hit enter.
  • Windows: press Windows, type "powershell", hit enter.
  • Linux: you know how.

In the terminal, type:

go version

You should see something like:

go version go1.22.0 darwin/arm64

The version number and the bit after will be different - that's fine. As long as you see "go version" and a number, Go is installed.

If you get command not found or something similar, the install didn't work. On macOS, try opening a new terminal window (the path might not have updated in the old one). On Linux, the package manager version may be old; download from go.dev/dl directly instead.

Step 2: Pick a folder for your code

You're going to write a lot of small programs. They need somewhere to live.

Pick anywhere on your computer you can find again. I'll use ~/code/go-learning/ in my examples. (The ~ means your home folder. On macOS that's /Users/yourname; on Linux it's /home/yourname; on Windows it's C:\Users\yourname.)

In your terminal, create the folder and go into it:

mkdir -p ~/code/go-learning
cd ~/code/go-learning

Then check you're there:

pwd

pwd means "print working directory" - it shows where you are. You should see your new folder's path.

Step 3: The smallest possible Go program

Open your text editor (VS Code, or whatever you chose). Create a new file. Save it as hello.go inside the go-learning folder you just made.

Type this into the file - type it, don't copy-paste:

package main

import "fmt"

func main() {
    fmt.Println("hello, world")
}

Save the file.

Step 4: Run it

Back in your terminal, in the same folder as hello.go, type:

go run hello.go

You should see:

hello, world

That's your first program. Take a moment.

What just happened - line by line

You typed five lines (plus a blank one). Each one matters:

  • package main - Every Go file says which "package" it belongs to. main is special: it's the package that becomes a runnable program. We'll come back to packages later; for now, just remember every program you write will start with this line.

  • import "fmt" - Pulls in the fmt package (short for "format"). fmt has functions for printing and formatting text. To use code that lives somewhere else, you have to import it first.

  • func main() - Defines a function called main. A "function" is a named block of code. main is special: it's the function that runs when the program starts. Every Go program needs exactly one main function in the main package.

  • fmt.Println("hello, world") - Calls the Println function (the "Pl" stands for "print line") from the fmt package, with "hello, world" as the input. Println prints whatever you give it, then moves to a new line.

  • The curly braces { and } mark the beginning and end of the function's body - the code that belongs to main.

Don't try to memorize this. You're going to type package main, import, func main() so many times in the next few sessions that it'll become automatic, like signing your name.

Try changing things

The way to learn is to break things on purpose and see what happens. Try each of these:

  1. Change "hello, world" to your name. Run again. Did it work?

  2. Add a second line that prints something else:

    fmt.Println("hello, world")
    fmt.Println("this is my second line")
    

  3. Try printing a number - no quotes:

    fmt.Println(42)
    

  4. Now break it on purpose. Remove the closing } and run. Read the error Go gives you. (Don't worry about understanding all of it - just notice that Go tells you which line is wrong.)

  5. Put the } back. Now mistype Println as println (lowercase p). Run. Read the error. Go is case-sensitive: Println and println are different names.

Reading errors is most of programming. Get comfortable seeing them. They are not scary; they are the computer telling you what it noticed.

What you might wonder

"Why all this scaffolding for one print line?" Fair question. In Python you'd write print("hello, world") and that's the whole file. Go trades typing for predictability: every Go program has the same shape, so you always know where to look. You'll appreciate this when programs get bigger.

"Do I need to compile it?" go run does compiling AND running in one step - it builds your program in memory, runs it, throws the result away. Later you'll meet go build, which produces a separate executable file you can ship to other computers.

"What does the func mean?" Short for "function." It's how Go marks "here begins a function definition." You'll write it a lot.

"Are tabs or spaces required for the indentation?" Go uses tabs by convention. There's a tool called gofmt that reformats your code; we'll meet it soon. For now, your editor probably does the right thing if you press Tab.

Done

You have: - Go installed. - A folder to put your code in. - One working program.

This was the boring infrastructure step. Next page is where the real learning starts.

Next: First real program →

Comments