github.com/DiversionCompany/notify@v0.9.9/example_readdcw_test.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  //go:build windows
     6  // +build windows
     7  
     8  package notify_test
     9  
    10  import (
    11  	"log"
    12  
    13  	"github.com/DiversionCompany/notify"
    14  )
    15  
    16  // This example shows how to watch directory-name changes in the working directory subtree.
    17  func ExampleWatch_windows() {
    18  	// Make the channel buffered to ensure no event is dropped. Notify will drop
    19  	// an event if the receiver is not able to keep up the sending pace.
    20  	c := make(chan notify.EventInfo, 4)
    21  
    22  	// Since notify package behaves exactly like ReadDirectoryChangesW function,
    23  	// we must register notify.FileNotifyChangeDirName filter and wait for one
    24  	// of FileAction* events.
    25  	if err := notify.Watch("./...", c, notify.FileNotifyChangeDirName); err != nil {
    26  		log.Fatal(err)
    27  	}
    28  	defer notify.Stop(c)
    29  
    30  	// Wait for actions.
    31  	for ei := range c {
    32  		switch ei.Event() {
    33  		case notify.FileActionAdded, notify.FileActionRenamedNewName:
    34  			log.Println("Created:", ei.Path())
    35  		case notify.FileActionRemoved, notify.FileActionRenamedOldName:
    36  			log.Println("Removed:", ei.Path())
    37  		case notify.FileActionModified:
    38  			panic("notify: unexpected action")
    39  		}
    40  	}
    41  }