Skip to content

Commit ccc981b

Browse files
alexeykazakovnathany
authored andcommitted
Check if channels are closed in the example (fsnotify#244)
* Check if channels are closed in the example * Check if the channels are closed before printing
1 parent 1f285c6 commit ccc981b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

example_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ func ExampleNewWatcher() {
2323
go func() {
2424
for {
2525
select {
26-
case event := <-watcher.Events:
26+
case event, ok := <-watcher.Events:
27+
if !ok {
28+
return
29+
}
2730
log.Println("event:", event)
2831
if event.Op&fsnotify.Write == fsnotify.Write {
2932
log.Println("modified file:", event.Name)
3033
}
31-
case err := <-watcher.Errors:
34+
case err, ok := <-watcher.Errors:
35+
if !ok {
36+
return
37+
}
3238
log.Println("error:", err)
3339
}
3440
}

0 commit comments

Comments
 (0)