Lesson 2 of 20 · Part 1: Getting started
Install Go and write your first program
Install Go on Windows, macOS or Linux, create a Go module and write, run and build your first Hello World program, explained line by line.
6 min read · Updated · Beginner friendly
What you'll learn
- Install Go and check that it works
- Create a project with go mod init
- Write, run and build a Hello World program
- Understand every line of a Go program
In this lesson you will set up Go on your computer and write your first program. By the end you will have a working project folder that you can reuse for every example in this tutorial.
No install? No problem
If you can’t install software right now, you can follow along in the Go Playground and come back to the setup later.
Step 1: Install Go
Download the installer for your system from the official site, go.dev/dl. Any version from Go 1.24 onwards works with this tutorial.
- Windows: run the
.msiinstaller and accept the defaults. - macOS: run the
.pkginstaller, or use Homebrew:brew install go. - Linux: follow the instructions on the download page, or use your package manager if it has a recent version.
Then open a new terminal window (Command Prompt, PowerShell or Terminal) and check the installation:
go versiongo version go1.27.4 windows/amd64Your version number and system will be different. That’s fine. If you see command not found or 'go' is not recognized, close the terminal and open a new one, or restart your computer, so it picks up the new settings.
Pick an editor
Any text editor works, but Visual Studio Code with the official Go extension is a great free choice. It highlights errors as you type, formats your code when you save and completes names for you.
Step 2: Create a project
A Go project is called a module. A module is simply a folder with a go.mod file in it that gives the project a name.
mkdir hello
cd hello
go mod init example.com/hellogo: creating new go.mod: module example.com/helloThe name example.com/hello is the module path. If you later publish the code, it is usually the repository address, such as github.com/yourname/hello. For learning, any name works.
Open the new go.mod file and you will see something like this:
module example.com/hello
go 1.27.4Step 3: Write the program
Create a file called main.go in the hello folder with this code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Step 4: Run it
From inside the hello folder, run:
go run .Hello, World!Congratulations, you have written and run a Go program! 🎉
go run . compiles the program in the current folder (.) and runs it straight away. Nothing is saved; it is the quickest way to try code.
Understanding every line
Let’s walk through the program from top to bottom.
package mainEvery Go file starts by saying which package it belongs to. A package is a group of related code. The name main is special: it tells Go that this package is a program you can run, not a library for other code to use.
import "fmt"import brings in code from another package. fmt (short for format, said “fumpt” or “f-m-t”) is part of Go’s standard library and contains functions for printing text and reading input.
func main() {
// ...
}func declares a function, a named block of code. The function called main in package main is where your program starts. When main finishes, the program ends. The code that belongs to the function goes between the curly braces { }.
fmt.Println("Hello, World!")This calls the Println function from the fmt package. It prints its argument and then a new line (“print line”). The text in double quotes is a string.
Notice what is not there: no semicolons at the end of lines and no boilerplate class around the code. Go adds the semicolons for you behind the scenes.
Why does Println start with a capital letter?
In Go, a name that starts with a capital letter is exported: other packages are allowed to use it. fmt.Println is capitalised so that your code can call it. You will see this rule all the time.
Printing more
Here are three printing functions you will use constantly:
package main
import "fmt"
func main() {
fmt.Println("Go", "is", "fun") // spaces between values, newline at the end
fmt.Print("no newline, ") // prints exactly what you give it
fmt.Print("so this continues\n") // \n is a newline character
fmt.Printf("%s is %d years old\n", "Go", 17)
}Go is fun
no newline, so this continues
Go is 17 years oldPrintf (“print formatted”) replaces each % placeholder with the next value: %s for a string, %d for a whole number. Lesson 4 covers more of them.
Comments
Text after // is a comment. Go ignores it; it is a note for people reading the code. For longer notes you can use /* ... */.
package main
import "fmt"
// main is where the program starts.
func main() {
/*
This prints a greeting.
Comments can span several lines like this.
*/
fmt.Println("Hi!") // a comment can also go at the end of a line
}Hi!A comment placed right above a function, starting with the function’s name, is a doc comment. Go’s tools use it to generate documentation.
Build an executable
go run is great while you work. When you want a program you can share, use go build:
go buildThis creates an executable file named after your module: hello.exe on Windows, hello on macOS and Linux. Run it directly:
./helloHello, World!On Windows, type .\hello.exe or just hello. The file contains everything it needs, so you can copy it to another computer with the same operating system and it will run there without Go installed.
Format your code automatically
Go has one official code style, and a tool applies it for you:
go fmt ./...It fixes indentation (Go uses tabs), spacing and alignment. Most editors run it every time you save. Because everyone’s Go code is formatted the same way, any Go project you open looks familiar.
Common mistakes
Putting the opening brace on its own line. Go needs the { on the same line as func, if or for:
package main
import "fmt"
func main()
{
fmt.Println("Hello")
}syntax error: unexpected semicolon or newline before {This happens because Go automatically inserts a semicolon at the end of the func main() line, which ends the statement too early.
Importing a package you don’t use. Unused imports are an error, not a warning:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Hello")
}"os" imported and not usedDelete the import or use it. Editors with the Go extension add and remove imports for you automatically.
Using single quotes for text. In Go, 'H' is a single character, not a string. Use double quotes: "Hello".
Calling the package something other than main. If your file says package hello, go run will complain that it is not a main package. Programs you run must use package main and have a func main().
Exercises
1. Introduce yourself. Change the program so it prints three lines: your name, where you live, and why you are learning Go.
Show solution
package main
import "fmt"
func main() {
fmt.Println("My name is Sam.")
fmt.Println("I live in Bengaluru.")
fmt.Println("I'm learning Go to build fast web services.")
}My name is Sam.
I live in Bengaluru.
I'm learning Go to build fast web services.2. Use Printf. Print the sentence Go was released in 2009 and has 25 keywords. using one fmt.Printf call, with the numbers passed as values rather than written in the text.
Show solution
package main
import "fmt"
func main() {
fmt.Printf("Go was released in %d and has %d keywords.\n", 2009, 25)
}Go was released in 2009 and has 25 keywords.3. Read the error. Move the { after func main() to the next line and run go run .. Read the message: it tells you the file, line and column of the problem. Put the brace back.
Recap
- Install Go from go.dev/dl and check it with
go version. go mod init <name>turns a folder into a Go module.- A runnable program needs
package mainand afunc main(). go run .compiles and runs;go buildcreates an executable;go fmtformats your code.fmt.Println,fmt.Printandfmt.Printfprint text.
Next, you will store values in variables and constants.
Finished this lesson?
Your progress is saved in this browser only.