Skip to content

Check if channels are closed in the example#244

Merged
nathany merged 3 commits intofsnotify:masterfrom
alexeykazakov:docs
Aug 30, 2018
Merged

Check if channels are closed in the example#244
nathany merged 3 commits intofsnotify:masterfrom
alexeykazakov:docs

Conversation

@alexeykazakov
Copy link
Copy Markdown
Contributor

This PR is just about adding a check for closed channels to the example https://github.com/fsnotify/fsnotify/blob/master/example_test.go

Without that check the goroutine will keep printing empty errors (nils) & events after the watcher is closed.

See #229

example_test.go Outdated
log.Println("modified file:", event.Name)
}
case err := <-watcher.Errors:
if !ok {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the ok before reading from the event. Essentially, this needs to happen before line 27.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right :)
Fixed.

}
case err, ok := <-watcher.Errors:
log.Println("error:", err)
if !ok {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, check for close before reading the error. :)

@nathany nathany merged commit ccc981b into fsnotify:master Aug 30, 2018
radu-munteanu pushed a commit to radu-munteanu/fsnotify that referenced this pull request Feb 25, 2019
* Check if channels are closed in the example

* Check if the channels are closed before printing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants