Go/
Lesson

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.

02

Package declaration

package main

Every 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.

AI pitfall
AI sometimes generates utility files with 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.
03

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 styleIdiomatic?Notes
import "fmt"Only for single importsFine when you need just one package
import (...) groupedYes, preferredStandard for 2+ packages
Separate import linesNogofmt converts these to grouped
import _ "pkg"Side-effect importRuns package init() without using exports
import . "fmt"DiscouragedPollutes namespace, avoid in production code
AI pitfall
AI frequently generates unused imports. Go's compiler rejects them outright: 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.
04

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.

IdentifierVisibilityExample
fmt.PrintlnExported (public)Accessible from any package
fmt.printlnWould not compileUnexported, can't access from outside fmt
myFuncUnexported (private)Only usable within its own package
MyFuncExported (public)Usable from any importing package
05

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.

Good to know
The binary name defaults to your file/module name. Use -o to specify a custom name: go build -o server main.go.
06

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 messageWhat AI did wrongFix
undefined: fmtForgot the importAdd import "fmt"
imported and not used: "os"Imported a package but never called itRemove the import
declared and not used: xCreated a variable but never read itUse it or remove it
syntax error: unexpected newlinePut opening brace on next lineMove { to same line as func
cannot refer to unexported nameCalled fmt.println (lowercase p)Use fmt.Println (uppercase P)
AI pitfall
AI models trained on C, Java, or C# code sometimes put opening braces on a new line in Go code. This is a syntax error in Go, the brace must be on the same line. If you see func main()\n{ in AI output, that's broken.
07

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 verbs

Println 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 automatically
08

gofmt: 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.

Good to know
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.
09

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.

Good to know
That 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.