Run Your First Go Program
Check your Go toolchain, initialize a module with go mod init, run code with go run, and compile a binary with go build. After this, you can build and execute any Go program from the terminal.
Course content
Sign in to save your progress.
-
Before You Write Code
-
Why Go Exists and When to Pick It
-
Run Your First Go Program
-
-
-
Values, Control Flow, and Functions
-
Variables, Types, and Zero Values
-
If, For Loops, and Multiple Return Values
-
-
-
Slices, Maps, and Structs
-
Arrays, Slices, and Why append Matters
-
Maps, Structs, and Pointer Receivers
-
-
-
Errors, Packages, and Testing
-
Handle Errors Without Hiding Them
-
Packages, Exports, and go test
-
-
-
Files, JSON, and a Small HTTP Server
-
Read Files and Decode JSON Structs
-
Build and Test a Small net/http Server
-
-
-
Pointers, Modules, and CLIs
-
Pointers and Memory Basics
-
Managing Dependencies and internal/ Packages
-
Building CLIs With flag, os.Getenv, and Exit Codes
-
-
Running your first Go program takes two minutes, but skipping one small step—creating a go.mod file—causes confusing errors as soon as your project grows to a second file. In this lesson, you will verify your Go toolchain, initialize a module, run a two-file program, and compile a standalone binary.
Skip ahead
If you already initialize projects with go mod init, run packages with go run ., and use go build, gofmt, and go vet, take the section quiz or move on to "Variables, Types, and Zero Values."
Step 1: Verify your Go version and PATH
Open a terminal and check that the go command is on your PATH:
$ go version
go version go1.27.4 linux/amd64
If your terminal prints command not found: go, the Go bin directory is missing from your shell's PATH (usually /usr/local/go/bin on Linux/macOS or C:\Program Files\Go\bin on Windows). Restarting the terminal after installing Go usually picks it up.
Step 2: Initialize a Go module
Create a fresh folder and run go mod init with a module path. If you plan to publish on GitHub later, use github.com/<you>/<repo>; for local practice, example.com/hello is standard:
$ mkdir hello && cd hello
$ go mod init example.com/hello
That writes a tiny go.mod file marking this folder as the root of your project:
module example.com/hello
go 1.27
Now create main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello from Go!")
}
$ go run .
Hello from Go!
Every piece here has one clear job:
package maintells Go this folder compiles into a runnable program rather than a reusable library package.import "fmt"loads the standard formatting package. Leave an import unused and Go refuses to compile.func main()is the entry point. It takes no arguments and returns nothing.
Step 3: Why go run . beats go run main.go
Watch what happens the moment you split a helper function into a second file in the exact same directory. Create greet.go right next to main.go:
// greet.go
package main
func formatGreeting(name string) string {
return "Welcome, " + name + "!"
}
// main.go
package main
import "fmt"
func main() {
fmt.Println(formatGreeting("Ana"))
}
Now compare running go run main.go against go run .:
$ go run main.go
# command-line-arguments
./main.go:6:14: undefined: formatGreeting
$ go run .
Welcome, Ana!
Passing a single filename (go run main.go) tells the compiler to ignore every other file in the folder! Passing . (the current package directory) compiles every .go file in package main together, so main.go sees formatGreeting from greet.go automatically.
Step 4: Compile a standalone binary with go build
go run . compiles into a temporary folder and deletes the executable when it finishes. When you want a binary you can keep or ship, run go build:
$ go build -o hello .
$ ./hello
Welcome, Ana!
That ./hello file is a native executable—it runs even if you copy it to another machine of the same OS/CPU architecture that does not have Go installed.
Step 5: Format and vet before you commit
Two built-in commands save hours of code-review noise and catch subtle bugs early:
gofmt -w .: Reformats every Go file in place to the universal Go indentation and spacing rules. Nobody argues about tabs vs. spaces in Go.go vet ./...: Static analyzer that catches bugs the compiler allows—like passing the wrong argument type tofmt.Printf("%d", "hello")or accidentally copying a mutex.
Common mistakes
- Skipping
go mod init: Runninggo run .without ago.modfails withgo: go.mod file not found in current directory or any parent directory. - Running
go run main.goin a multi-file package: Fails withundefined: ...for every function living in sibling files. Always usego run .. - Forgetting to save
main.goinpackage main: Naming the packagepackage hellocompiles cleanly withgo build—and produces no executable binary at all, because Go treats it as a library!
💡 Tip
Configure your editor (VS Code with the official Go extension, or GoLand) to run goimports on save. It formats your file like gofmt and automatically adds or removes imports as you type.
What happens if you skip this step
If you treat Go files like loose Python scripts without go.mod and go run ., everything breaks the first time you split code across two files, add a dependency, or run go test ./....
You split a helper function into utils.go inside package main, run go run main.go, and get undefined: helperName. What command fixes it without changing any code?
About
Check your Go toolchain, initialize a module with go mod init, run code with go run, and compile a binary with go build. After this, you can build and execute any Go program from the terminal.
- Duration
- 5 min
- Last update
- 11/27/2026
Comments
Sign in to commentNo comments yet.