Golang if err != nil Checks in 5 Minutes
The if err != nil check in Go for beginners, in five minutes, on a real Go program: why printing an error is not handling it (a real nil pointer dereference), returning early, the usual shape of checks inside a function, adding context with fmt.Errorf and %w, and testing for a kind of error with errors.Is, which %v quietly breaks.
Printed, then crashed
Your Go program found the error, printed it, and crashed anyway. Joe's flight school tool loads the day's schedule, but Jess, an instructor, moved the file. Joe's check caught it: if err!= nil, print the error. Most people would call that handled. Then the next line ran, and the program crashed with a nil pointer panic. It tried to count flights in a schedule that wasn't there. In five minutes, you'll see what that check really needs to do, and the one-letter mistake that quietly breaks it later.
sed -n '25,31p' main.gofunc main() { s, err := loadSchedule("schedule.json") if err != nil { fmt.Println("could not load:", err) } fmt.Println("Flights today:", len(s.Flights)) }
go run .could not load: open schedule.json: no such file or directory panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x115530] goroutine 1 [running]: main.main() /home/joe/flights/main.go:30 +0x80 exit status 2
What the check is for
Think of a ticket gate at a train station. When your card is rejected, the gate beeps and stays shut. A gate that beeped and then opened anyway would be useless. Joe's check beeped. It never shut the gate.

So what is the check really for? A Go function that can fail hands back a value and an error. In Go, nil means nothing. If the error is nil, nothing went wrong, and the value is good to use. If it isn't nil, the value is junk: here the schedule is nil, meaning no schedule at all. So the check has one job: stop before anything touches that value.

Return early
The fix is one word: return. That's the gate staying shut: we're inside main, so returning ends the program right there, after printing the problem. Every line after the check can trust the schedule.

Run it, and there's one clean line, and no crash. Then Jess puts the file back, with a stray comma at the end of the list. Now the message says: invalid character, looking for beginning of value. Invalid where? In which file?
go run .could not load: open schedule.json: no such file or directory
cat schedule.json{"flights": ["N172 09:00 Jess", "N152 11:30 Joe", "N172 14:00 Jess",]}
go run .could not load: invalid character ']' looking for beginning of value
The answer is inside loadSchedule, the function that reads the file and decodes it. After each step there's a check, and each one hands the error straight back to main, untouched. The second check is written on one line: it runs the decode and tests its error in the same if. Nothing adds the file name, so the message arrives with no clue where it came from.
sed -n '13,23p' main.gofunc loadSchedule(path string) (*Schedule, error) { data, err := os.ReadFile(path) if err != nil { return nil, err } var s Schedule if err := json.Unmarshal(data, &s); err != nil { return nil, err } return &s, nil }
Wrap with %w
So Joe adds a note to each error before handing it back to main. fmt.Errorf builds a new message around the old error. In it, %s is a slot for the file name, and %w is a slot that keeps the original error inside, whole. The same run now says: parse schedule.json, invalid character. Jess deletes the comma, and the program prints: flights today, three.
sed -n '13,23p' main.gofunc loadSchedule(path string) (*Schedule, error) { data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read schedule: %w", err) } var s Schedule if err := json.Unmarshal(data, &s); err != nil { return nil, fmt.Errorf("parse %s: %w", path, err) } return &s, nil }
go run .could not load: parse schedule.json: invalid character ']' looking for beginning of value
go run .Flights today: 3
The errors package's own documentation calls this wrapping. And it gives you a function, errors.Is, that looks through the wrapped errors for a match.

Check the kind with errors.Is
But not every error should stop the program. On a brand-new laptop there's simply no schedule yet, and that's fine. So Joe asks errors.Is: is this error, or anything wrapped inside it, Go's missing-file error, fs.ErrNotExist? Because he wrapped with %w, it can see through to the original. If it matches, he swaps the junk value for a real, empty schedule. Run it: no schedule yet, flights today, zero.
sed -n '27,37p' main.gofunc main() { s, err := loadSchedule("schedule.json") if errors.Is(err, fs.ErrNotExist) { fmt.Println("no schedule yet, starting an empty one") s, err = &Schedule{}, nil } if err != nil { fmt.Println("could not load:", err) return } fmt.Println("Flights today:", len(s.Flights))
go run .no schedule yet, starting an empty one Flights today: 0
Here's the one-letter mistake I promised. In both Errorf lines, %w becomes %v, which only prints an error as text. The message reads the same. Guess first: does errors.Is still find it? No. %v copies the text, but drops the original error, so errors.Is misses it, and the new laptop quits with an error instead of starting an empty schedule.
grep -n Errorf main.go18: return nil, fmt.Errorf("read schedule: %v", err) 22: return nil, fmt.Errorf("parse %s: %v", path, err)
go run .could not load: read schedule: open schedule.json: no such file or directory
Four habits for if err!= nil. Check right after the call. Return, don't just print. Add context with %w. And to check the kind of error, use errors.Is: you just saw the text stay the same while the real error was gone.

Recap
So: Joe's check printed and kept going. Now it stops, says where it failed, and knows a missing file from a broken one, as long as it's %w, not %v.

If the check makes sense now, a like helps YouTube show this to more beginners, and subscribing brings the next Go idiom: method receivers.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
- The Go Programming Language Specification: If statements (a simple statement before the condition) and Run-time panics
- Package fmt, Errorf and the %w verb
- Package errors, wrapping and Is
- Package os, ReadFile: https://pkg.go.dev/os#ReadFile and package encoding/json, Unmarshal
- The Go Blog, Working with Errors in Go 1.13
Demos: one container on our own Mac standing in for a laptop; the flight school is made up. Every command, error and number was real. Watch the full video on YouTube.