← Back to Go Course | Chapter 1: Setup & Basics | Lesson 3 of 7

Your First Go Program

Your first Go program is a tiny set of instructions that tells the computer to print a greeting on the screen.

The package main Declaration

Every Go source file starts with a package declaration. To produce a standalone, runnable program, the file must belong to 'package main' -- this tells the Go tool that this package is an entry point rather than a reusable library. If you name it anything else, 'go build' will produce a library archive instead of an executable.

Note: Library code (meant to be imported, not run directly) uses a different package name, like 'package mathutils'.

Example: The package main Declaration

markup
package main

import "fmt"

func main() {
	fmt.Println("package main marks this as a runnable program")
}

The main Function

Inside package main, Go looks for a function literally named main with no parameters and no return value -- this is where execution begins. Everything that happens when you run the program is triggered from inside, or reachable from, this single function.

Example: The main Function

markup
package main

import "fmt"

func main() {
	fmt.Println("Execution starts here, inside func main()")
}

Importing and Using fmt

The fmt package (short for format) is part of Go's standard library and provides functions for formatted input/output, most commonly fmt.Println for printing a line and fmt.Printf for printing with format verbs like %s or %d. You bring it into scope with an import statement at the top of the file.

Note: An unused import is a compile-time error in Go, not just a linter warning -- this keeps codebases free of dead imports.

Example: Importing and Using fmt

markup
package main

import "fmt"

func main() {
	name := "Gopher"
	fmt.Printf("Hello, %s! Welcome to Go.\n", name)
}

Running Your Program

The 'go run' command compiles your source file into a temporary binary and immediately executes it, printing the output to your terminal. This is the fastest way to try out small programs while learning, without manually managing build artifacts.

Note: 'go run' is great for quick iteration; use 'go build' when you want a reusable binary file.

Example: Running Your Program

bash
go run hello.go

⚠️ Run this command in your terminal.

Common Mistakes
  1. Naming the file's package something other than main and then being confused why 'go run' produces no executable.
  2. Forgetting the func main() entry point -- Go programs won't compile without exactly one main function in package main.
  3. Leaving an import statement in the file for a package that is never used, which Go treats as a compile error, not a warning.
Chapter Summary
  • Every runnable Go program lives in 'package main' and defines a 'func main()' as its entry point.
  • The fmt package provides functions like Println and Printf for printing output.
  • Go requires every imported package to actually be used, or the program won't compile.
  • 'go run file.go' compiles and executes a program in one step.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.