Time to write and run real Go. In this lesson you create a module, write a
Hello World program in main.go, run it with go run, and build a standalone
binary with go build — while learning what each line does.
Create a module
A Go project is a module, described by a go.mod file. Make an empty folder,
open a terminal in it, and initialise the module:
mkdir hello
cd hello
go mod init example/hello
That command writes a small go.mod file recording the module path and the Go
version:
module example/hello
go 1.24
The module path (example/hello) is just an identifier. For a project you plan
to publish, you would use its repository path, such as
github.com/you/hello.
Write main.go
Create a file named main.go next to go.mod and type in this complete,
runnable program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
That is a full Go program. Save the file — the next section runs it.
Understand each part
Every line above has a job:
package maindeclares that this file belongs to themainpackage. A program you can run must have amainpackage; any other name produces a library instead of an executable.import "fmt"pulls in the standard library’sfmtpackage, which handles formatted input and output.func main()is the entry point. When you run the program, Go callsmainfirst; when it returns, the program ends.fmt.Println(...)prints its argument followed by a newline.
Run it with go run
From the module folder, run the program with:
go run .
The . tells Go to build and run the package in the current directory. You will
see:
Hello, World!
go run compiles to a temporary location and runs immediately — perfect while
you are experimenting.
Build a binary with go build
When you want a file you can share or deploy, compile it instead:
go build
This produces an executable in the current folder — hello on macOS and Linux,
hello.exe on Windows — named after the module. Run it directly:
./hello
The output is the same Hello, World!, but now it is a self-contained binary
with no Go installation required to run it.
Common mistakes
- Wrong package name. If the first line is not
package main,go runandgo buildwill not create a runnable program. - Missing the import. Using
fmt.Printlnwithoutimport "fmt"fails to compile. Conversely, importing a package you never use is also an error. - Running from the wrong folder.
go run .andgo buildmust run inside the module directory that holdsgo.modandmain.go.
Practice
Starting from the same module, change the program so it prints two lines:
Hello, World! and then I am writing Go..
Show the solution
Add a second fmt.Println call inside main:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
fmt.Println("I am writing Go.")
}Run it with go run .. Each Println prints its text on its own line, so the
program outputs the two messages in order.
Summary
- A Go project is a module created with
go mod init <path>. - A runnable program needs
package mainand afunc mainentry point. - Use
go run .to run during development andgo buildto produce a binary.
Now that you can run code, learn how to store and use values with variables and types in Go. You can also revisit the full Go course index.