When you ask AI to generate a Go program, this is what it produces as the starting point. Every Go developer needs to read this structure fluently, not because you'll type it from memory, but because you'll evaluate AI-generated Go code hundreds of times and need to spot what's right, what's wrong, and what's missing.
The anatomy of hello world
AI will generate something like this:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Five lines. Every one matters. Let's break them down so you can evaluate them instantly when you see AI output.
Package declaration
package mainEvery Go file must start with a package declaration. The name main is special, it tells Go "this is an executable program, not a library." Any other name (like package utils) builds a library that other programs import.
package main when they should be package utils or package handlers. If every file in your project says package main, you can't import functions between them. Watch for this when AI scaffolds multi-file projects.The import statement
import "fmt"This brings in Go's formatting package for printing, scanning, and string formatting. It's part of the standard libraryWhat is standard library?A collection of ready-made tools that come built into a language - no install required. Covers common tasks like reading files or making web requests., no installation required.
When you need multiple packages, group them with parentheses:
import (
"fmt"
"time"
"strings"
)This is the idiomatic style. gofmt automatically converts separate import statements into a grouped block.
| Import style | Idiomatic? | Notes |
|---|---|---|
import "fmt" | Only for single imports | Fine when you need just one package |
import (...) grouped | Yes, preferred | Standard for 2+ packages |
Separate import lines | No | gofmt converts these to grouped |
import _ "pkg" | Side-effect import | Runs package init() without using exports |
import . "fmt" | Discouraged | Pollutes namespace, avoid in production code |
imported and not used: "os". This is one of Go's strictest rules, every import must be used. When reviewing AI output, check imports against actual usage.The main function
func main() {
fmt.Println("Hello, World!")
}func declares a function. main is the reserved entry point, Go looks for this in package main to start execution. The opening brace must be on the same line as func, Go's parser requires this.
The capital P in Println is significant: in Go, exported (public) identifiers start with uppercase. Lowercase means package-private. This is a language rule, not a convention.
| Identifier | Visibility | Example |
|---|---|---|
fmt.Println | Exported (public) | Accessible from any package |
fmt.println | Would not compile | Unexported, can't access from outside fmt |
myFunc | Unexported (private) | Only usable within its own package |
MyFunc | Exported (public) | Usable from any importing package |
Running vs. building
Two commands, two different purposes:
# Compile and run in one step - great for development
$ go run main.go
Hello, World!
# Compile to a standalone binary - for deployment
$ go build -o myapp main.go
$ ./myapp
Hello, World!go run compiles to a temporary binaryWhat is binary?A ready-to-run file produced by the compiler. You can send it to any computer and it just works - no install needed., runs it, and cleans up. Fast feedback during development. go build produces a permanent binary you can copy anywhere, no Go installation needed on the target machine.
-o to specify a custom name: go build -o server main.go.The strict compilerWhat is compiler?A program that translates code you write into a language your computer can execute. It also catches errors before your code runs. in action
Go's compiler catches problems that dynamic languages let slip through. When AI generates code with issues, Go tells you immediately:
| Error message | What AI did wrong | Fix |
|---|---|---|
undefined: fmt | Forgot the import | Add import "fmt" |
imported and not used: "os" | Imported a package but never called it | Remove the import |
declared and not used: x | Created a variable but never read it | Use it or remove it |
syntax error: unexpected newline | Put opening brace on next line | Move { to same line as func |
cannot refer to unexported name | Called fmt.println (lowercase p) | Use fmt.Println (uppercase P) |
func main()\n{ in AI output, that's broken.fmt's three print functions
AI will use these interchangeably. Know the differences so you can evaluate which one fits:
fmt.Print("no newline") // Prints, no trailing newline
fmt.Println("with newline") // Prints + adds newline
fmt.Printf("Hello %s\n", name) // Formatted output with verbsPrintln is the most common for quick output. Printf is what you'll see in serious code because it gives precise control over formatting. The \n in Printf is manual, it doesn't add one automatically like Println.
fmt.Println("The answer is", 42)
// Output: The answer is 42
// Println adds spaces between arguments automaticallygofmt: the end of style debates
Go has an official formatter. Run gofmt -w main.go and it rewrites your file to standard Go style. Tabs for indentation. Braces on the same line. Consistent spacing. Every Go codebase on the planet looks the same.
Most editors run gofmt on save. This means when AI generates Go code and you paste it into your editor, it gets auto-formatted. No manual cleanup of spacing or indentation.
gofmt only changes whitespace, never logic. It's always safe to run. But it can be surprising the first time, if AI generated code with spaces for indentation, gofmt converts them to tabs.A slightly more realistic program
When you ask AI to generate a Go program that uses multiple packages, it should produce something like this:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Current time:", now.Format("2006-01-02 15:04:05"))
fmt.Println("Day of week:", now.Weekday())
}No pip install, no npm install, the time package is part of Go's standard libraryWhat is standard library?A collection of ready-made tools that come built into a language - no install required. Covers common tasks like reading files or making web requests.. Evaluate this code: package declaration, grouped import, main function, exported functions with uppercase names, := short declaration. Every piece follows the patterns from this lesson.
2006-01-02 15:04:05 format string looks bizarre. Go uses a reference time (Mon Jan 2 15:04:05 MST 2006) as a template instead of YYYY-MM-DD. The numbers 1, 2, 3, 4, 5, 6, 7 map to month, day, hour, minute, second, year, timezone. AI often gets this wrong, watch for it.