Hello, World
The smallest runnable Go program - package main, the main function, and fmt.Println.
The smallest complete Go program shows how Go starts a program: it runs a function named main inside a package also named main.
Go files in one directory share a package name. package main tells Go to build an executable, and func main is where that program starts. The fmt package is included with Go and provides printing functions. fmt.Println writes text followed by a newline.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Run it directly with go run:
OUTPUT
$ go run hello.go
Hello, World!Or compile it into a binary first:
OUTPUT
$ go build -o hello hello.go
$ ./hello
Hello, World!In production
For a small program, fmt.Println is enough. Larger programs often use a logger, a tool that labels messages as information, warnings, or errors.
Enjoyed this? Get more software craft delivered to your inbox.