Hello, World in Go
A minimal tour to get started quickly.
Setup
- Install Go from
https://golang.org/dl. The Go toolchain includes the compiler, standard library, and thegocommand. - Initialize a module in a new folder (enables dependencies and reproducible builds). A module is the unit of versioning and distribution in Go:
go mod init example.com/hellogo env GOMODshows the activego.mod
First program
Every executable program starts in package main with a func main(). Imports use full module or standard library paths. The compiler and linker produce a static binary by default.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Run it: go run . or build: go build then ./hello.
go run .compiles and runs the package in one step (handy during development)go buildproduces a native binary (name comes from folder by default), which you can distribute or run later
Print utilities
The fmt package provides formatted I/O. Print writes without a newline, Println appends a newline and spaces between arguments, and Printf formats according to verbs (e.g., %d, %s).
fmt.Print("no newline")
fmt.Println("with newline")
fmt.Printf("%s %d\n", "answer", 42)
Printfuses format verbs (like C’s printf); remember to include\n
Project layout tip
- One module per repo is typical; create subpackages with folders and their own
packagename to factor code by dependency boundaries - Use
go list allto see packages;go doc fmt.Printlnto read docs;go helpshows built-in help
Formatting and static checks
go fmt ./...formats code; most editors run it on save. Go style is enforced by tooling—avoid manual bikeshedding.go vet ./...catches suspicious constructs (e.g., printf format mismatches, copylocks).golangci-lintadds extra linters (optional) for deeper checks (ineffassign, staticcheck, etc.).
Functions
func add(a, b int) int { return a + b }
- Types follow names; multiple params of the same type can share the type (
a, b int). - Go does not support default parameters or overloading; prefer multiple small functions or option structs.
Next steps
- Explore
if,for,functions,errors - Learn modules and dependencies with
go getand semantic import versions
Summary
package main+func main()is the entry point- Use
fmtfor printing;go runto execute quickly;go buildcreates a binary go fmtandgo vetkeep code consistent and safe