Skip to content

Beginner · Getting started with Go · topic 3 of 46 · 30 min

Your first program and Go modules

Explanation

A Go project is a module: a folder with a go.mod file that names the project and the Go version it needs. Every runnable program has package main and a func main, where it starts. You create a module with go mod init and run it with go run .

Packages and modules

A package is a folder of .go files that belong together. Every file starts with a package line. A module is a whole project: one or more packages with a go.mod file at the top. The module has a name called the module path, usually where the code lives online, like github.com/asha/expenses. For learning, any name like example.com/hello is fine.

Think of it like this: A module is like a school. Packages are the classrooms inside it. go.mod is the school's registration certificate: it says the school's name and which rules (Go version) it follows.

Step by step

bash
mkdir hello
cd hello
go mod init example.com/hello
go mod init creates go.mod in the folder.
output
module example.com/hello

go 1.24
go.mod: the module path, and the minimum Go version the code needs. Your number may differ.

Now create a file named main.go in the same folder:

go
package main

import "fmt"

func main() {
	fmt.Println("Namaste, Go!")
}
Run it with go run . and it prints Namaste, Go!

Every line explained

  • package main: this package becomes a runnable program, not a library. Only package main can be run.
  • import "fmt": use the standard package fmt (short for format), which prints and formats text.
  • func main(): the function where the program starts. When main returns, the program ends.
  • fmt.Println(...): call the function Println from package fmt. It prints its values separated by spaces and then a new line.
  • Braces { } hold the body. The opening brace must be on the same line as func main(); putting it on the next line is a compile error.
  • No semicolons: Go adds them for you at the end of lines, which is why the brace rule exists.

Printing: Println, Print and Printf

go
package main

import "fmt"

func main() {
	fmt.Println("Total:", 250, "rupees")
	fmt.Print("no newline ")
	fmt.Print("here\n")
	fmt.Printf("%s scored %d marks (%.1f%%)\n", "Asha", 452, 90.4)
}
Prints: Total: 250 rupees / no newline here / Asha scored 452 marks (90.4%).
VerbMeaningExample output
%vthe value in a default format42, [1 2 3], {Asha 21}
%dan integer452
%sa stringAsha
%qa quoted string"Asha"
%.2fa float with 2 decimals118.00
%Tthe type of the valueint, string, []int
%%a literal percent sign%

go.sum

When you add a dependency (another module), Go creates go.sum next to go.mod. It stores a checksum (a fingerprint) of every dependency, so a changed or tampered download is detected. Commit both go.mod and go.sum to Git. You will add dependencies in module 5.

Common mistakes

  • "go: cannot find main module": you ran go run outside a folder with go.mod. Run go mod init first, or cd into the project.
  • "package hello is not a main package" style errors: a runnable program must say package main.
  • Unused imports are compile errors. If you import "os" and never use it, the build fails with "os" imported and not used. Remove it (most editors do this for you).
  • Capital letters matter: fmt.println does not exist; it is fmt.Println. Names starting with a capital letter are the ones a package exports.
Tip: Run go build once and look at the folder: you get hello.exe on Windows or hello on Linux/macOS. That file runs on any computer with the same operating system and processor, without Go installed.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Your first program and Go modules · 1/7

Your first Go program

Time to write your first Go program. First we make a module, which is a Go project.

Prefer a video by a teacher? Find free videos on “go hello world go mod init tutorial” on YouTube ↗

In real life: A canteen bill printer

A college canteen wants a tiny program that prints today's bill. It is a module named example.com/canteen with one main.go. go build gives one canteen.exe that the manager double-clicks on the billing laptop, which does not need Go installed.

go
package main

import "fmt"

func main() {
	samosa, chai := 2, 3
	total := samosa*20 + chai*15
	fmt.Println("Samosa x", samosa, "= Rs", samosa*20)
	fmt.Println("Chai x", chai, "= Rs", chai*15)
	fmt.Printf("Total: Rs %d\n", total)
}
Prints Samosa x 2 = Rs 40, Chai x 3 = Rs 45, Total: Rs 85.

Practice (0/4 done)

Task 1 · Question

What does this print? fmt.Println("Rs", 250, "only")

Task 2 · Question

This file does not compile. Why? package main import ( "fmt" "os" ) func main() { fmt.Println("hi") }

Task 3 · Question

What does fmt.Printf("%T", 3.5) print?

Task 4 · Practice

Create a module called example.com/greet. Write main.go that prints your name, your city and a line like "Total fees: Rs 45000" using Printf with %d.

Quick quiz

Question 1

1.Which file marks a folder as a Go module?

Question 2

2.Where does a Go program start running?

Question 3

3.What is go.sum for?

Question 4

4.Which Printf verb prints a value in its default format?

Question 5

5.You write the opening brace of func main on the next line. What happens?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which file marks a folder as a Go module?

Answer: go.mod

go.mod holds the module path and Go version and marks the module root.

Where does a Go program start running?

Answer: func main in package main

The entry point of every Go program is func main in package main.

What is go.sum for?

Answer: It stores checksums of dependencies so tampered downloads are detected

go.sum holds a cryptographic checksum for every dependency version used.

Which Printf verb prints a value in its default format?

Answer: %v

%v works for any value: numbers, strings, slices, maps and structs.

You write the opening brace of func main on the next line. What happens?

Answer: Compile error, because Go inserts a semicolon at the end of the func line

Go automatically inserts semicolons at line ends, so the brace must be on the same line as func main().

How does Go choose a dependency version by default?Related

Answer: The minimal version that satisfies all requirements

Minimal version selection keeps builds reproducible.

From Dependencies with Go modules

What is the default visibility of items in a module?Related

Answer: Private

Items are private unless marked pub.

From Packages, crates and modules

Where does a cargo project keep its main code?Related

Answer: src/main.rs

cargo new puts the starting code in src/main.rs.

From Your first program and cargo

What is the usual package name for files in folder internal/store?Related

Answer: store

By convention the package name matches the last part of the folder path.

From Packages, visibility and project layout

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Getting started with Go” before its exam: Variables, basic types and zero values, Constants, iota and type conversions.
  2. Module 1 exam: Getting started with Go · 14 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Control flow and functions. 46 topics left in the course before the final exam.

← Previous: Installing Go and the go command