Skip to Content
Skip to content
Go for Beginners: Write Reliable Programs and Small Services
Beginner
Courses
Go for Beginners: Write Reliable Programs and Small Services

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.

Course Home

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 main tells 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 to fmt.Printf("%d", "hello") or accidentally copying a mutex.

Common mistakes

  • Skipping go mod init: Running go run . without a go.mod fails with go: go.mod file not found in current directory or any parent directory.
  • Running go run main.go in a multi-file package: Fails with undefined: ... for every function living in sibling files. Always use go run ..
  • Forgetting to save main.go in package main: Naming the package package hello compiles cleanly with go 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
Was this helpful?
​ 0 ​ 0

Comments

Sign in to comment

No comments yet.