func (w *Watcher) AddPath(root string) error {
	w.paths[root] = true
	info, err := os.Lstat(root)
	if err != nil {
		return err
	}
	if info.Mode().IsRegular() {
		root = filepath.Dir(root)
		if w.dirs[root] {
			return nil
		}
		if err := w.watcher.Add(root); err != nil {
			return err
		}
		w.dirs[root] = true
	} else if info.Mode().IsDir() && w.recursive {
		return WalkDir(DirFS("."), filepath.Clean(root), func(path string, d DirEntry, err error) error {
			if err != nil {
				return err
			}
			if d.IsDir() {
				if w.dirs[path] {
					return SkipDir
				}
				if err := w.watcher.Add(path); err != nil {
					return err
				}
				w.dirs[path] = true
			}
			return nil
		})
	}
	return nil
}
AddPath adds a new path to watch.
package main
import (
	"os"
	"path/filepath"
	"time"
	"github.com/fsnotify/fsnotify"
)
type Watcher struct {
	watcher   *fsnotify.Watcher
	dirs      map[string]bool
	paths     map[string]bool
	recursive bool
}
func NewWatcher(recursive bool) (*Watcher, error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	return &Watcher{watcher, map[string]bool{}, map[string]bool{}, recursive}, nil
}
func (w *Watcher) Close() error {
	return w.watcher.Close()
}