Automate the Boring Stuff with Go, Ch. 0.1: The Terminal
Part of Automate the Boring Stuff with Go, a beginner-friendly guide to programming in Go.
Part 0 is for readers who have never used a terminal or git. If you have, skip to chapter 1; it will still be here.
Every chapter in this book says “open a terminal and type”. The terminal is the window where you talk to your computer in text: you type a command, press Enter, it does the thing and prints what happened. Programmers live in it because it is faster than clicking once you know a dozen words, and because every program you will write in this book is run from it.
This chapter is those dozen words. Nothing here is Go; all of it is what Go, git and every other tool assume you already know.
Opening it
- Windows: press the Start key, type
Terminal, press Enter. On Windows 11 that opens Windows Terminal running PowerShell. On Windows 10 typePowerShellinstead. Either way you are in PowerShell, and that is the shell this book uses on Windows. (Later chapters mention Git Bash, which git installs; it is a second, Unix-style shell, and you will meet it in 0.3.) - macOS: press Cmd+Space, type
Terminal, press Enter. It is also in Applications, Utilities. The shell is zsh. - Linux: Ctrl+Alt+T on most desktops, or find Terminal in the applications menu. The shell is almost always bash.
You get a window with a line of text ending in a symbol and a blinking cursor. That line is the prompt. It is the computer saying “your turn”. On Windows it looks like PS C:\Users\you>, on macOS like you@mac ~ %, on Linux like you@laptop:~$. The part before the symbol usually tells you which folder you are in, which brings us to the first idea.
You are always somewhere
The terminal always has a current folder (programmers say directory; the words mean the same thing). Every command you run happens in that folder unless you say otherwise. Ask where you are:
pwd
/Users/t/Desktop/peter-lankton/automate-the-boring-stuff-with-go
pwd means “print working directory”. It works on all three systems; on Windows it prints something like C:\Users\you. When you open a terminal you start in your home folder, the one with your Desktop and Documents in it.
List what is in the current folder:
ls
LICENSE
README.md
ch1
ch10
...
go.mod
go.sum
tools
ls works everywhere too (on Windows it is a nickname for Get-ChildItem, and dir does the same). That listing is the book’s code folder, which you will have on your own machine by the end of 0.4.
Moving around
cd changes the current folder. Give it a name from the listing:
cd ch1
pwd
/Users/t/Desktop/peter-lankton/automate-the-boring-stuff-with-go/ch1
Two special names: .. is the folder above, ~ is your home folder.
cd ..
pwd
cd ~
pwd
/Users/t/Desktop/peter-lankton/automate-the-boring-stuff-with-go
/Users/t
Three habits that make this painless:
- Press Tab. Type
cd ch1and press Tab: the shell completes the name, or shows the choices if several match. You almost never type a full folder name. Tab completion also saves you from typos. - Press the up arrow to bring back the last command, and keep pressing for older ones. Edit it and press Enter.
- Type
clearwhen the window fills up (clsalso works on Windows). It only clears the screen; nothing is undone.
Each system has a way to jump the terminal into a folder you are looking at. On macOS, drag a folder from Finder onto the Terminal window and its path appears at the cursor; open . goes the other way and opens the current folder in Finder. On Windows, Shift+right-click inside a folder in Explorer gives “Open in Terminal”. On Linux most file managers have “Open in Terminal” on the right-click menu.
Running a program
A command is just a program’s name. The first one you will run constantly:
go version
go version go1.27.1 darwin/arm64
Words after the name are arguments, the program’s instructions. version is an argument to go. In chapter 1 you will type go run ., which is two arguments: run, and ., the current folder. Most of this book’s commands follow that shape: a program, a verb, a thing to act on.
go run ./ch1/hello
Hello, world!
A program that is still running when you want it to stop: press Ctrl+C. That works on every system (on macOS it is Control, not Command) and it is how you stop chapter 3’s Rock, Paper, Scissors, chapter 13’s scheduler, and anything else that would otherwise run forever.
A program that is waiting for you to type (chapter 3’s games do this): type your answer and press Enter. If you are stuck inside something that shows : or (END) at the bottom of the screen, that is a pager showing you a long result; press q to leave it. You will meet it in 0.3 with git log.
Reading an error
Type something that is not a program:
nosuchcommand
zsh: command not found: nosuchcommand
On Windows the same mistake prints a longer sentence that begins The term 'nosuchcommand' is not recognized as the name of a cmdlet. On Linux, bash: nosuchcommand: command not found.
“Command not found” means one of three things: you misspelled it, the program is not installed, or it is installed somewhere the terminal does not look. The next chapter explains that last case, because it is the one that bites everyone right after they install Go.
The other error you will see this week is “no such file or directory” (Windows: “Cannot find path”). It means the folder or file you named does not exist from where you are standing. Run pwd and ls, and the mystery usually ends.
The whole vocabulary
| You want to | Windows (PowerShell) | macOS and Linux |
|---|---|---|
| see where you are | pwd | pwd |
| list the folder | ls or dir | ls |
| list everything, including hidden files | ls -Force | ls -a |
| go into a folder | cd name | cd name |
| go up one | cd .. | cd .. |
| go home | cd ~ | cd ~ |
| make a folder | mkdir name | mkdir name |
| clear the screen | clear | clear |
| stop a running program | Ctrl+C | Ctrl+C |
| leave a pager | q | q |
Ten commands. Everything else the book asks you to type is a program, usually go or git, with its arguments.
Conclusion
You can open a terminal, tell where you are, move, list, run a program, stop it, and read the two errors that account for most of a beginner’s first week. Next: what a path is, and why the computer sometimes cannot find a program you just installed.