Your first Go program

Create a Go module, write a runnable Hello World in main.go, then run and build it while learning package main, imports and the func main entry point.

Prerequisites

Learning objectives

  • Create a Go module with go mod init
  • Write and run a Hello World program
  • Understand package main, imports and func main

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 main declares that this file belongs to the main package. A program you can run must have a main package; any other name produces a library instead of an executable.
  • import "fmt" pulls in the standard library’s fmt package, which handles formatted input and output.
  • func main() is the entry point. When you run the program, Go calls main first; 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 run and go build will not create a runnable program.
  • Missing the import. Using fmt.Println without import "fmt" fails to compile. Conversely, importing a package you never use is also an error.
  • Running from the wrong folder. go run . and go build must run inside the module directory that holds go.mod and main.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 main and a func main entry point.
  • Use go run . to run during development and go build to 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.

Your progress is stored only in this browser. No account is required.

Search

Search runs entirely in your browser.