]> go.fuhry.dev Git - fsnotify.git/commitdiff
Move example usage to the readme
authorNathan Youngman <4566+nathany@users.noreply.github.com>
Wed, 11 Mar 2020 16:36:45 +0000 (10:36 -0600)
committerNathan Youngman <4566+nathany@users.noreply.github.com>
Wed, 11 Mar 2020 17:35:18 +0000 (11:35 -0600)
may resolve #328

README.md
example_test.go [deleted file]

index 06de57947443cf85d14baa0867819afc43dccb5b..b2629e5229ca4dcccc53539e0e94c3a763b7aebb 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/example_test.go b/example_test.go
deleted file mode 100644 (file)
index b4f9f95..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2012 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build !plan9
-
-package fsnotify_test
-
-import (
-       "log"
-
-       "github.com/fsnotify/fsnotify"
-)
-
-func ExampleNewWatcher() {
-       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
-}