Your First Program
Scope: starting a Go module, writing a program, running it two ways, and reading the files involved. All commands below were run and verified.
Make a project
mkdir hello
cd hello
go mod init hello
Check: go mod init prints go: creating new go.mod: module
hello and creates a file called go.mod:
module hello
go 1.25
go.mod marks this folder as a Go project (a module). The
module name (hello here) is just a label for now; for published
libraries it becomes the import path.
Write the program
Create main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Three things to notice:
package main— every runnable program starts with this line. It says "this file belongs to the main package," which is Go's word for "this builds into a program, not a library."import "fmt"—fmtis the formatting/printing library, part of Go's standard library. Unused imports are a compile error in Go — the compiler makes you clean up after yourself.func main()— where the program starts. CapitalPrintlnbecause exported (public) names start uppercase — more on that in Functions.
Run it
go run .
Check: prints Hello, world!
The . means "the package in this folder." go run compiles to
a temporary file, runs it, and throws it away — perfect while
developing.
Build it
go build .
Check: a file named hello (or hello.exe on Windows)
appears in the folder. Run it directly:
./hello # Mac/Linux
.\hello.exe # Windows PowerShell
That's the single binary Go is known for: one file, no interpreter, no dependencies to install alongside it. Copy it to another machine with the same OS and it just runs.
Change it and re-run
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
fmt.Println("Go builds fast.")
}
Run go run . again.
Check: both lines print. Notice the speed — Go compiles small programs in well under a second. That fast cycle is a deliberate feature, not an accident.
gofmt: the formatter
Go has one official code style, enforced by a tool:
gofmt -l .
If it prints filenames, those files aren't formatted. Fix them:
gofmt -w .
Most editors run this on save automatically. Don't argue with the formatter — the entire Go world looks the same, which means any Go code you read feels familiar.
Check
- [ ]
go mod init,go run ., andgo build .all work - [ ] You can say what
go.modis for - [ ] You can explain why
import "fmt"is there and what happens if you don't use it
Next: Variables and Types.