Writing Hello, World
Create a file called main.go:
package main
import "fmt"
func main() { fmt.Println("Hello, World!")}Running the Program
go run main.goOutput:
Hello, World!Understanding Each Line
package main
Every Go file starts with a package declaration. The main package is special — it tells Go that this is an executable program, not a library.
import "fmt"
The import keyword brings in packages from the standard library or third-party modules. fmt stands for “format” and provides functions for formatted I/O.
func main()
This is the entry point of every Go program. When you run your program, Go looks for this function inside package main and starts execution there.
fmt.Println()
Println prints a line to standard output, followed by a newline character.
Compiling to a Binary
# Build a binarygo build -o hello
# Run the binary./hello # Linux/macOShello.exe # WindowsThe go build command compiles your code into a standalone binary. No runtime or virtual machine required — just a single executable file.
Go’s Philosophy in Action
Even in this tiny program, you can see key Go design principles:
- Explicit imports — No magic, everything you use is declared
- Simple syntax — No semicolons, no classes, no inheritance
- Opinionated formatting — Run
go fmtto auto-format your code
Try It Yourself
- Change the message to print your name
- Print multiple lines using multiple
fmt.Println()calls - Use
fmt.Print()instead and notice the difference (no newline)
Next Step
Now let’s dive into basic types and variables in Go.