Environment & Hello World
Setting Up Go
1. Install Go
Download the installer for your OS from https://go.dev/dl (current LTS: 1.22+).
After installation verify:
go version
# go version go1.22.3 linux/amd642. VS Code Setup
Install the Go extension by the Go Team at Google (extension id: golang.go).
On first open VS Code will prompt you to install tools — click Install All. This pulls in gopls, dlv (debugger), staticcheck, and friends.
3. Your First Program
Create a directory and enter it:
mkdir hello && cd hello
go mod init example.com/helloCreate main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Run it without producing a binary:
go run main.go
# Hello, World!Build a native binary:
go build -o hello .
./hello
# Hello, World!4. What Just Happened
Every Go source file begins with a package declaration. The main package is special — its main() function is the program entry point. The import block pulls in packages from the standard library or external modules. fmt is Go's formatting package.
Go compiles to a statically linked binary — no runtime or VM needed on the target machine. That is why Go binaries are often used for CLI tools and microservices.
5. go mod init
go mod init creates go.mod, which declares the module path and the minimum Go version. Think of it as Go's package.json. You will not need to touch it manually for simple programs, but understanding it is essential for multi-file projects.
module example.com/hello
go 1.226. Formatting
Go ships with an official formatter. Run it at any time:
go fmt ./...The VS Code extension runs this on save automatically.