Skip to content
T2Run
Esc
  • RunPad Text Editor
    A Notepad++-style editor in your browser: tabs, split view, compare, 30 languages, JSON/XML/YAML tools.
    Text
  • Diff Checker
    Compare two texts or files side by side and see every added, removed and changed line.
    Text
  • JSON Beautifier
    Format, validate, sort and minify JSON with precise error locations.
    JSON & Data
  • Face Map Studio
    Map, chart and document botulinum toxin treatments on 3D face maps of your patient’s photos, with guided clinical photography.
    Health & Clinic
  • Photo Editor & Passport Photo Maker
    Edit photos with pro adjustments, filters, crop, text and background removal — plus passport photos for 25+ countries.
    Image
  • Image Resizer
    Batch resize photos by pixels or percent and export to JPG, PNG or WebP.
    Image
  • JSON Validator
    Check if JSON is valid and see the exact line and column of any error.
    JSON & Data
  • JSON Minifier
    Remove whitespace to make JSON compact, and see how many bytes you saved.
    JSON & Data

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 .msi installer and accept the defaults.
  • macOS: run the .pkg installer, 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:

Terminal
go version
Output
go version go1.27.4 windows/amd64

Your 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.

Terminal
mkdir hello
cd hello
go mod init example.com/hello
Output
go: creating new go.mod: module example.com/hello

The 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:

go.mod
module example.com/hello

go 1.27.4

Step 3: Write the program

Create a file called main.go in the hello folder with this code:

main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}

Step 4: Run it

From inside the hello folder, run:

Terminal
go run .
Output
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.

Go
package main

Every 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.

Go
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.

Go
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 { }.

Go
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:

Go
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)
}
Output
Go is fun
no newline, so this continues
Go is 17 years old

Printf (“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 /* ... */.

Go
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
}
Output
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:

Terminal
go build

This creates an executable file named after your module: hello.exe on Windows, hello on macOS and Linux. Run it directly:

Terminal
./hello
Output
Hello, 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:

Terminal
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:

Go
package main

import "fmt"

func main()
{
    fmt.Println("Hello")
}
Compiler error
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:

Go
package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("Hello")
}
Compiler error
"os" imported and not used

Delete 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
Go
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.")
}
Output
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
Go
package main

import "fmt"

func main() {
	fmt.Printf("Go was released in %d and has %d keywords.\n", 2009, 25)
}
Output
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 main and a func main().
  • go run . compiles and runs; go build creates an executable; go fmt formats your code.
  • fmt.Println, fmt.Print and fmt.Printf print text.

Next, you will store values in variables and constants.

Finished this lesson?

Your progress is saved in this browser only.