// Homework 33, problem 2: Sentinel errors. // // The library is a map from a book title to whether it is out on loan. Declare // two sentinel errors, errNoSuchBook and errAlreadyOut. Write borrow: // // - a title that is not in the map returns errNoSuchBook, wrapped with %w, // with the title after it: "no such book: Hamlet" // - a book that is already out returns errAlreadyOut // - otherwise it marks the book as out and returns nil // // Then change main: use errors.Is to print a friendly line for each sentinel, // as shown in the solutions, in place of the plain "Error:" line. // // You will need to add "errors" to the imports. // // Run with: go run exercises/33/homework/02-library.go package main import "fmt" func borrow(library map[string]bool, title string) error { // TODO: check the title, then mark the book as out. return nil } func main() { library := map[string]bool{"Dune": false, "Emma": false, "Ulysses": true} for _, title := range []string{"Dune", "Dune", "Hamlet", "Ulysses"} { err := borrow(library, title) if err != nil { // TODO: tell the two errors apart with errors.Is. fmt.Println("Error:", err) continue } fmt.Printf("You borrowed %s.\n", title) } }