Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Shadowing
- Declaring a new variable with the same name in an inner block, hiding the outer one there.
- Typed nil
- An interface value that holds a type and a nil pointer; it is not equal to nil.
- Sentinel error
- A package-level error value, such as ErrNoSkill, that callers test for with errors.Is.
- errors.Join
- Retains non-nil child errors in a tree; all-nil input returns nil.
An error is a value with structure
Go’s error is an interface with an Error() string method. An error result is an ordinary value, so declarations, assignments and returns determine which value reaches the caller. An interface can carry a dynamic type as well as its concrete value; inspect both when reasoning about success and failure.
Wrapping preserves an underlying error while adding context. errors.Is tests a failure against a target, and errors.As finds a matching error type through retained causes. A joined error can retain several failures, giving callers structure to inspect beyond a combined message.
01Time slots
Which parse result reaches the booking desk?
A repair café books visitors for a time slot and checks the items they bring. The desk needs a failed parse to reach its caller, a repairable item to count as success, and a multi-item visit to retain every refusal.
save parses “ten” as an hour inside an if block and returns err at the end. Predict the result.
err is declared with var at the top; inside the block, hour, err := strconv.Atoi(slot).
package main
import (
"fmt"
"strconv"
)
func save(slot string) error {
var err error
if slot != "" {
hour, err := strconv.Atoi(slot)
if err == nil {
fmt.Println("hour", hour)
}
}
return err
}
func main() {
fmt.Println("result:", save("ten"))
}
go run main.go
Use a fresh main.go for each experiment and run go run main.go. This track uses Go 1.27.3 with a 64-bit int; the lab’s strconv.Atoi messages follow that source profile.
02Success or failure
What does the caller compare with nil?
check returns a *BookingError variable that was never set. The caller compares the result with nil. Predict both lines.
check’s result type is error; it returns a *BookingError that is nil.
package main
import "fmt"
type BookingError struct {
Reason string
}
func (e *BookingError) Error() string {
return e.Reason
}
func check(name string) error {
var problem *BookingError
if name == "" {
problem = &BookingError{Reason: "no name"}
}
return problem
}
func main() {
err := check("ana")
fmt.Println("err == nil:", err == nil)
fmt.Printf("%v %T\n", err, err)
}
03A whole visit
Can the desk inspect both refusals?
A visit has two problems. They are combined with errors.Join, printed, and checked with errors.Is. Predict the output.
Each failure wraps its own sentinel with %w.
package main
import (
"errors"
"fmt"
)
var ErrNoSkill = errors.New("no repairer for this item")
var ErrNoParts = errors.New("parts not in stock")
func main() {
err := errors.Join(
fmt.Errorf("toaster: %w", ErrNoSkill),
fmt.Errorf("lamp: %w", ErrNoParts),
)
fmt.Println(err)
fmt.Println("no skill:", errors.Is(err, ErrNoSkill))
fmt.Println("no parts:", errors.Is(err, ErrNoParts))
}
04Repair the repair café
Keep the parse error, return a real nil, keep every failure
The booking desk needs three repairs. Let a bad slot reach main, keep successful item checks successful, and report every refused item in a multi-item visit.
The lab file contains its imports, the reader, the supplied types and data, and a complete main.
Repair the repair café
Shadowing, the typed nil, then several errors at once.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?