In this post, you will learn the basic structure of a Go (Golang) hello world program. We will go through each part of the code so you understand how a simple Go program is built and executed.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

Package statement in Go
A package in Go groups related code together and helps you organize your project. The package clause must appear at the top of the file, and it can only be preceded by comments. The name main tells the Go runtime that this code builds into an executable program.
If you use any other package name, such as mathutil, Go treats it as a library package instead of a runnable program. In short, use package main when you want to build a Go hello world program that you can execute directly.
Import statement in Golang
The import statement lets you use functions and types from other packages, such as Go’s standard library or your own packages. It also declares the dependencies your program needs during the build step. Go does not import anything by default, so you must explicitly list every package you want to use.
When you have multiple imports, you usually group them using parentheses, with one package per line:
import (
"fmt"
"os"
"math"
)
If you import a package and do not use it in your Go code, the Go tools will complain. When you run go fmt or build the program, unused imports are removed or reported as errors, which keeps your dependencies clean.
Main function in a Go hello world program
The func keyword defines a function in Go. The main function inside the main package is a special entry point. Program execution always starts from this function in a Go hello world example or any other Go executable.
func main() {
}
Inside the main function, we can call fmt.Println to print text to the console, which is exactly what we do in a Go hello world program:
func main() {
fmt.Println("Hello, World!")
}
The fmt package provides formatted I/O (input/output) functions. The Println function prints the given string followed by a newline character, so the output appears on its own line in the terminal.
- Go does not require semicolons at the end of each line, even though the compiler inserts them internally.
- The Go toolchain provides
gofmt(orgo fmt) to format your code. It enforces a consistent style and indentation, so Go source code looks the same across all platforms and editors.
How to build and run a Go hello world program
You can build your Go hello world program into an executable binary using the go build command. Run this command from the directory that contains your hello-world.go file:
go build .\hello-world.go
This command generates a single binary file as output. On Windows, the file will usually be named hello-world.exe. The binary is built for the platform and architecture on which you run the command.
Go also makes it easy to build your hello world program for other platforms by setting the GOOS and GOARCH environment variables before running go build:
$env:GOOS="darwin"; $env:GOARCH="arm64"; go build hello-world.go
If you only want to run the Go hello world program without creating a separate binary file, you can use go run:
go run hello-world.go
This command compiles and runs the program in a single step, which is very convenient while learning Golang or testing small examples.
Conclusion: your first Go hello world program
You have now seen how a simple Golang hello world program is structured, how the package main and main function work, and how to use the import statement. With these basics in place, you are ready to explore more Go features, write your own functions, and build more complex programs.



