// Chapter 31 example: a missing file is not always a problem. // It reads name.txt from the folder you run it from, if there is one. // Run with: go run exercises/31/07-missing-file.go package main import ( "errors" "fmt" "os" "strings" ) // loadName returns the name in name.txt, or "World" if there is no such file. func loadName() (string, error) { data, err := os.ReadFile("name.txt") if errors.Is(err, os.ErrNotExist) { return "World", nil } if err != nil { return "", err } return strings.TrimSpace(string(data)), nil } func main() { name, err := loadName() if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Hello, %s!\n", name) }