You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson walks you through installing Go, setting up your workspace, creating your first module, and writing, building, and running your first Go program.
Go can be installed from go.dev/dl. Downloads are available for all major platforms:
| Platform | Installation Method |
|---|---|
| macOS | .pkg installer or brew install go |
| Windows | .msi installer or choco install golang |
| Linux | Extract the tarball to /usr/local |
After installing, open a terminal and run:
go version
You should see output like:
go version go1.22.0 linux/amd64
go env GOPATH # Where Go stores downloaded modules (~/ go by default)
go env GOROOT # Where Go itself is installed
go env GOOS # Target operating system
go env GOARCH # Target architecture
Go uses modules to manage dependencies. Every Go project starts with a module.
mkdir hello
cd hello
go mod init example.com/hello
This creates a go.mod file:
module example.com/hello
go 1.22
The module path (example.com/hello) is typically the repository URL where the code will be hosted (e.g., github.com/yourname/hello).
Create a file called main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
| Element | Purpose |
|---|---|
package main | Declares this file belongs to the main package — the entry point for an executable |
import "fmt" | Imports the fmt package for formatted I/O |
func main() | The entry point of the program — execution starts here |
fmt.Println | Prints a line to standard output |
You have two options:
go runCompile and run in one step (great for development):
go run main.go
Output:
Hello, World!
go buildCompile to a binary, then run it:
go build -o hello
./hello
The -o hello flag specifies the output binary name. On Windows, use hello.exe.
Go makes cross-compilation trivial. Set GOOS and GOARCH:
# Build for Linux on AMD64
GOOS=linux GOARCH=amd64 go build -o hello-linux
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.