FileWatcher is a Go package that provides a file and directory watcher with callbacks for create, remove, and modify events.
To use FileWatcher in your Go project, you can install it using go get
:
go get -u github.com/NIR3X/filewatcher
package main
import (
"fmt"
"time"
"github.com/NIR3X/filewatcher"
)
func main() {
// Initialize FileWatcher
f := filewatcher.NewFileWatcher(time.Second, func(path string, isDir bool, failed func()) {
// Callback for created files and directories
if isDir {
fmt.Println("created dir:", path)
} else {
fmt.Println("created file:", path)
}
}, func(path string, isDir bool) {
// Callback for removed files and directories
if isDir {
fmt.Println("removed dir:", path)
} else {
fmt.Println("removed file:", path)
}
}, func(path string, isDir bool, failed func()) {
// Callback for modified files and directories
if isDir {
fmt.Println("modified dir:", path)
} else {
fmt.Println("modified file:", path)
}
})
defer f.Close()
// Watch the current directory
err := f.Watch(".")
if err != nil {
fmt.Println("Error:", err)
return
}
// Keep the application running to receive file events
select {}
}
NewFileWatcher
: Create a new instance of the FileWatcher.Close
: Close the FileWatcher and stop watching.Watch
: Start watching a specified file or directory.Unwatch
: Stop watching a specified file or directory.
This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.