mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 17:07:18 +08:00
58 lines
815 B
Go
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
|
|
}
|