Your First Go Program
In this page:
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
package main
import "fmt"
func main() {
fmt.Println("package main marks this as a runnable program")
}
Login to try C/C++/Java/PHP code in the editor
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
package main
import "fmt"
func main() {
fmt.Println("Execution starts here, inside func main()")
}
Login to try C/C++/Java/PHP code in the editor
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
package main
import "fmt"
func main() {
name := "Gopher"
fmt.Printf("Hello, %s! Welcome to Go.\n", name)
}
Login to try C/C++/Java/PHP code in the editor
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
go run hello.go
⚠️ Run this command in your terminal.
- Naming the file's package something other than main and then being confused why 'go run' produces no executable.
- Forgetting the func main() entry point -- Go programs won't compile without exactly one main function in package main.
- Leaving an import statement in the file for a package that is never used, which Go treats as a compile error, not a warning.
- 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: