Skip to content

Commit

Permalink
Move example usage to the readme
Browse files Browse the repository at this point in the history
may resolve fsnotify#328
  • Loading branch information
nathany authored and tirmatovcl committed May 6, 2020
1 parent 90421f5 commit c697cec
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,53 @@ All [releases](https://github.com/fsnotify/fsnotify/releases) are tagged based o

Go 1.6 supports dependencies located in the `vendor/` folder. Unless you are creating a library, it is recommended that you copy fsnotify into `vendor/github.com/fsnotify/fsnotify` within your project, and likewise for `golang.org/x/sys`.

## Usage

```go
package main

import (
"log"

"github.com/fsnotify/fsnotify"
)

func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()

err = watcher.Add("/tmp/foo")
if err != nil {
log.Fatal(err)
}
<-done
}
```

## Contributing

Please refer to [CONTRIBUTING][] before opening an issue or pull request.
Expand Down

0 comments on commit c697cec

Please sign in to comment.