Hello World in Go
Your first Go program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Go program.
The Code
Create a file named hello.go:
| |
Understanding the Code
package main- Declares this file as part of the main package. Executable programs must have a main package.import "fmt"- Imports the format package from Go’s standard library for formatted I/O.func main()- The entry point of every Go executable. Execution starts here.fmt.Println()- Prints a line to standard output followed by a newline.
Running with Docker
The easiest way to run this without installing Go locally:
| |
Running Locally
If you have Go installed:
| |
Expected Output
Hello, World!
Key Concepts
- Go is compiled - Source code is compiled directly to machine code (no VM)
- Package system - Every Go file belongs to a package
- main is special - The
mainfunction in themainpackage is the entry point - Automatic formatting - Run
go fmt hello.goto format your code (or usegofmt) - Static linking - Go produces a single binary with no external dependencies
Why Go Is Different
Unlike Java or Python, Go compiles to a single static binary. This means:
- No runtime dependencies needed
- Easy deployment (just copy the binary)
- Fast startup time
Next Steps
Continue to Variables and Data Types to learn about storing and manipulating data in Go.
Running Today
Last updated: