
Your first Go program is a five-line file that prints Hello, World! to your terminal. Getting there takes an empty folder and a handful of commands: install Go, confirm it works, make a project folder, initialize a module, write main.go and run it. This guide walks through each step in order and explains what every line does along the way.
Go was created at Google partly because engineers were tired of waiting for big programs to compile, and that impatience shows in the language. It builds fast, runs fast and reads clean. Hello, World is a modest finish line, but if it prints, your whole setup works. That is the real prize.
- Install Go from go.dev, then open a fresh terminal and run go version.
- Make a lowercase folder, step inside it and run go mod init hello.
- Write main.go with package main, import "fmt" and func main().
- Run it with go run main.go and look for Hello, World! on its own line.
- Most first errors come from an unused import, PATH, the wrong folder or a lowercase println.
- 0:00Intro
- 0:33What you'll build and need
- 1:16Install Go from go.dev
- 2:12Confirm Go with go version
- 3:02Create a home for your project
- 3:57Initialize the module with go mod init
- 4:59Write main.go, your first program
- 5:59Unpack the three lines that matter
- 6:52Run it with go run main.go
- 7:35Peek at what go run does
- 8:31Verify you see Hello, World!
- 9:24Fix the most common first errors
- 10:11Untangle the PATH problem
- 11:06Level up after Hello, World
- 12:10Everything you just did
- 12:43Package, import, main: master the trio
Gather your tools and install Go from go.dev
The shopping list is short. You need a computer running Mac, Windows or Linux, a text editor you like, a terminal window and the Go installer from go.dev. You don't need prior Go knowledge, an account or paid tools, and there's no setup marathon. If you can download a file and type a command, you're qualified.
Go to go.dev and click the download button. The site usually detects your system, but check that you picked the right file, especially on Mac, where Apple Silicon and Intel get different installers. Run the installer and accept the defaults. They're sensible. On Linux you usually unpack an archive instead, and go.dev gives you the exact command.
Next comes the step that trips people up most: restart your terminal. A terminal window that was already open has no idea Go just arrived. Close it and open a new one. Think of it as the coworker who missed the memo. You won't convince them, so hire a fresh one.
- Installer matches your operating system and chip
- Default options accepted
- Every old terminal window closed, and a new one opened
Confirm the install with go version
Before you build anything, make sure Go is actually installed. Type go version and press enter. You should see a line that starts with go version, followed by a version number, your operating system and your chip. Treat it as your installation receipt. Your numbers will differ from the ones in the video, and that's fine, because any reasonably recent version works for this tutorial.
If the version prints, Go is installed and your terminal can find it. If you get command not found, your terminal can't locate Go yet. That's usually a PATH issue or a terminal that was open before you installed. Nothing is broken. The troubleshooting section below fixes it properly.
- Output starts with go version
- No command not found error
# in a fresh terminal
go version
# you should see something like:
go version go1.x darwin/arm64
Create a folder for your project
Every Go project gets its own folder. It's like giving each project its own desk instead of piling everything on the kitchen table. In your terminal, type mkdir hello to create the folder, then cd hello to move into it. Finally, run pwd to print your current location so you can confirm you're inside hello. These commands work in the default terminal on Mac and Linux and in PowerShell on Windows.
Keep the name simple: lowercase letters and no spaces. hello beats My Go Stuff, because spaces force you to put quotes around paths, and that gets old fast. Then stay put. Every command from here on runs from this folder. If something later complains that a file is missing, check which folder your terminal is in first.
- Folder name is lowercase with no spaces
- pwd shows you're inside hello
# make a folder and move into it
mkdir hello
cd hello
# confirm where you are
pwd
Initialize the module with go mod init
Now tell Go this folder holds a real project. Type go mod init hello. Go replies that it's creating a new go.mod file. Open that file and you'll see module hello at the top, with the Go version your project expects underneath. Older tutorials often skip this step, but modern Go expects every project to be a module, and leaving it out causes many errors with today's tools.
Think of go.mod as your project's name tag. It tells every Go tool which project it's working on. Later, when you add packages written by other people, go.mod also records them and their versions. Hello, World doesn't need any, so the file stays tiny. Projects you plan to share often use a longer name, such as github.com/you/hello.
- A go.mod file now exists in the hello folder
- It begins with module hello
go mod init hello
# creates a go.mod file:
module hello
go 1.x
Write main.go, your first program
Create a file called main.go inside hello and open it in your editor. This is where setup ends and programming begins. The program is five short lines: package main, then import "fmt", then func main() with an opening curly brace, then fmt.Println("Hello, World!") inside it, and a closing brace. Type it yourself instead of pasting. Your fingers remember things your clipboard won't.
Two details catch nearly everyone. First, spelling is exact. Println has a capital P, and Go is case-sensitive, so lowercase println won't be found. The message also needs double quotes, not single ones. Second, brace placement matters. The opening curly brace must sit on the same line as func main(). Move it to the next line and Go refuses to compile, which surprises people coming from other languages.
- File is named main.go
- Println has a capital P
- Message is in double quotes
- Opening brace is on the func main() line
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Understand package, import and func main
Before running anything, look at what you just typed, because this shape appears in every Go program, from tiny scripts to large servers. Think of a letter: who it's for, the tools you borrowed to write it and where the reading starts. Once you can name what each part does, you can open almost any Go file and find your way around.
package main tells Go this file builds a program you can run, not a library for other code. Only package main produces something you can launch from the terminal. import "fmt" brings in Go's built-in tools for formatting and printing text. Some people say "fumpt" and some say "format", and both are fine. Without that line, there's no Println to call.
func main() is the front door. Execution starts there, Go runs the code inside from top to bottom, and when main finishes, the program ends. That's the whole lifecycle of Hello, World. It's a short trip, but every bigger program follows the same route.
- package main: this file builds a runnable program
- import "fmt": borrows printing and formatting tools
- func main(): where execution starts and ends
Run it with go run main.go and check the output
Save the file, switch back to your terminal, confirm you're still in hello and type go run main.go. That one command does two jobs: it compiles your code and runs it immediately. Behind the scenes, Go builds a temporary binary, starts func main, calls fmt.Println and prints Hello, World! to your terminal. Then it deletes the temporary binary, like a guest who washes the dishes before leaving.
Go is compiled, not interpreted. It turns your whole program into machine code before running it, which is a big reason Go programs are fast. It also catches many mistakes before anything runs. The compiler is quick enough that go run usually feels instant, so you get compiled speed without waiting around.
Now check the result carefully. You want Hello, World! on its own line, with the same comma, the same exclamation mark and no error lines around it. Then prove the program is really yours: change the message to Hello, Gopher!, save and run it again. Go fans call themselves Gophers after the language's blue mascot, so you've earned the title.
- File saved before running, because go run reads from disk, not your editor
- Output matches Hello, World! exactly
- An edited message shows up after you save and rerun
$ go run main.go
Hello, World!
# edit the message, save, run again
$ go run main.go
Hello, Gopher!
Fix the most common first errors, including PATH
First programs don't always work the first time, and that's normal. Four errors cover most first-day problems. An unused import ("fmt" imported and not used) happens because Go refuses to build when you import something you don't use. Either use fmt or delete the line. go: command not found means you need to fix PATH and restart the terminal. stat main.go: no such file means you're in the wrong folder, so cd into hello. A lowercase println needs a capital P.
Read error messages from the top. Go names the file and line number, such as line four of main.go. Go straight there, because the problem is usually sitting right on that line.
Command not found deserves extra attention, because it makes beginners think they broke something. PATH is the list of folders your terminal searches when you type a command, so think of it as the terminal's address book. If Go's bin folder isn't listed, the terminal never looks there. Start by closing every terminal window, including the one inside your editor, and opening a new one. Old windows keep using the old PATH. If that doesn't fix it, add Go's bin folder to PATH using the line shown on the go.dev install page, restart and run go version again.
- Unused import: use fmt or delete it
- Command not found: restart the terminal, then check PATH
- No such file: cd into hello
- Undefined println: capitalize the P
Level up with go fmt, go build and go vet
Once Hello, World works, you already own Go's toolbox. go fmt rewrites your file in the official Go style, spacing included. Every Go project uses the same format, so nobody argues about it. go build works like go run but keeps the result. It leaves a program file in your folder that you can run again or give to a friend with the same kind of computer, even if they don't have Go installed.
go vet acts like a code reviewer. It flags code that compiles but is probably a mistake, such as a print format that doesn't match its values. For structured practice, the Tour of Go offers free interactive lessons on go.dev that run in your browser. The first three tools come with Go, so you don't need to install anything. Pick one and try it this week.
- go fmt: tidy formatting in one command
- go build: a standalone program you can share
- go vet: catches suspicious code early
- Tour of Go: free lessons in the browser
What to remember
- Install Go from go.dev, open a fresh terminal and confirm it with go version.
- Give each project its own simply named folder and run go mod init inside it.
- Every Go program starts with package main, an import and func main().
- go run compiles and runs in one step, but it only sees saved files.
- Go's error messages name the file and line, so start your fix there.
- go fmt, go build and go vet are ready to use as soon as Go is installed.
Questions people ask
Why does my terminal say go: command not found after I installed Go?
Your terminal can't find Go's bin folder in its PATH, usually because the window was open before you installed. Close every terminal, including the one in your editor, and open a new one. If that doesn't work, add Go's bin folder to PATH using the line from the go.dev install page.
Do I really need to run go mod init for Hello, World?
Yes. Modern Go expects every project to be a module, and skipping this step causes many errors with today's tools. It takes one command and creates a small go.mod file that names your project.
Why won't Go compile when I put the opening brace on a new line?
Go requires the opening curly brace to be on the same line as the function declaration, such as func main() {. It's a deliberate rule of the language, and it often surprises people coming from other languages.
What is the difference between go run and go build?
go run compiles your program into a temporary binary, runs it and then deletes it. go build compiles the program and keeps the result as a file you can run again or share with someone who has the same kind of computer.
Why does Go complain that "fmt" is imported and not used?
Go refuses to build if you import a package you don't use. Either call something from fmt, such as Println, or delete the import line.
Which version of Go do I need for this tutorial?
Any reasonably recent version works, so you don't need the very latest release. If go version prints a version number, you're ready to go.