Beginner · Getting started with Go · topic 3 of 46 · 30 min
Your first program and Go modules
Explanation
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.
Step by step
mkdir hello
cd hello
go mod init example.com/hellomodule example.com/hello
go 1.24Now create a file named main.go in the same folder:
package main
import "fmt"
func main() {
fmt.Println("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
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)
}| Verb | Meaning | Example output |
|---|---|---|
| %v | the value in a default format | 42, [1 2 3], {Asha 21} |
| %d | an integer | 452 |
| %s | a string | Asha |
| %q | a quoted string | "Asha" |
| %.2f | a float with 2 decimals | 118.00 |
| %T | the type of the value | int, 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.
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
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.
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)
}Practice (0/4 done)
What does this print? fmt.Println("Rs", 250, "only")
This file does not compile. Why? package main import ( "fmt" "os" ) func main() { fmt.Println("hi") }
What does fmt.Printf("%T", 3.5) print?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Variables, basic types and zero valuesGetting started with Go · same module
- Dependencies with Go modulesPackages, testing and generics
- Packages, crates and modulesRust Programming · Modules, crates, traits and generics
- Your first program and cargoRust Programming · Getting started with Rust
- Packages, visibility and project layoutPackages, testing and generics
- Installing the JDK and your first programJava Programming (Core to Spring Boot) · Getting started with Java
Questions people ask about this
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.
What is the default visibility of items in a module?Related
Where does a cargo project keep its main code?Related
Answer: src/main.rs
cargo new puts the starting code in src/main.rs.
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.
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
- 2 more topics in “Getting started with Go” before its exam: Variables, basic types and zero values, Constants, iota and type conversions.
- Module 1 exam: Getting started with Go · 14 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Control flow and functions. 46 topics left in the course before the final exam.