// Chapter 34 example: code that writes files, tested in a temporary folder. // // Run its tests from this folder: // cd exercises/34/examples/04-temp-dir // go test -v package notes import ( "os" "strings" ) // Save writes the notes to a file, one per line. func Save(filename string, notes []string) error { text := strings.Join(notes, "\n") + "\n" return os.WriteFile(filename, []byte(text), 0644) } // Load reads the notes back. Empty lines are skipped. func Load(filename string) ([]string, error) { data, err := os.ReadFile(filename) if err != nil { return nil, err } notes := []string{} for _, line := range strings.Split(string(data), "\n") { line = strings.TrimSpace(line) if line != "" { notes = append(notes, line) } } return notes, nil }