100-go-mistakes/04-control-structures/35-defer-loop/main.go
2021-12-27 15:57:20 +01:00

58 lines
815 B
Go

package main
import "os"
func readFiles1(ch <-chan string) error {
for path := range ch {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
}
return nil
}
func readFiles2(ch <-chan string) error {
for path := range ch {
if err := readFile(path); err != nil {
return err
}
}
return nil
}
func readFile(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
return nil
}
func readFiles3(ch <-chan string) error {
for path := range ch {
err := func() error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Do something with file
return nil
}()
if err != nil {
return err
}
}
return nil
}