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
.pkgfile. Double-click; follow the prompts. - Windows: download the
.msifile. Double-click; follow the prompts. - Linux: either download the
.tar.gzfrom go.dev/dl and follow the instructions there, or use your distro's package manager:sudo apt install golang-goon Debian/Ubuntu,sudo dnf install golangon 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:
You should see something like:
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:
Then check you're there:
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:
Save the file.
Step 4: Run it¶
Back in your terminal, in the same folder as hello.go, type:
You should see:
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.mainis 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 thefmtpackage (short for "format").fmthas functions for printing and formatting text. To use code that lives somewhere else, you have toimportit first. -
func main()- Defines a function calledmain. A "function" is a named block of code.mainis special: it's the function that runs when the program starts. Every Go program needs exactly onemainfunction in themainpackage. -
fmt.Println("hello, world")- Calls thePrintlnfunction (the "Pl" stands for "print line") from thefmtpackage, with"hello, world"as the input.Printlnprints 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 tomain.
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:
-
Change
"hello, world"to your name. Run again. Did it work? -
Add a second line that prints something else:
-
Try printing a number - no quotes:
-
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.) -
Put the
}back. Now mistypePrintlnasprintln(lowercasep). Run. Read the error. Go is case-sensitive:Printlnandprintlnare 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.