{"id":4166,"date":"2018-11-04T11:32:42","date_gmt":"2018-11-04T03:32:42","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4166"},"modified":"2018-11-04T11:32:42","modified_gmt":"2018-11-04T03:32:42","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a016-%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e6%96%87%e4%bb%b6%e5%8f%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4166.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#16-\u5982\u4f55\u68c0\u6d4b\u6587\u4ef6\u53d8\u5316"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u4e4b\u524d\u5728Linux\u4e0b\u4f7f\u7528\u8fc7inotify-tools\u6765\u83b7\u77e5\u6587\u4ef6\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u5728\u535a\u5ba2\u91cc\u4e5f\u8bb0\u5f55\u4e86<a href=\"https:\/\/ixyzero.com\/blog\/archives\/tag\/inotify\" target=\"_blank\" rel=\"noopener\">\u51e0\u7bc7\u6587\u7ae0<\/a>\uff0c\u73b0\u5728\u5728\u5b66\u4e60Go \u8bed\u8a00\uff0c\u4e5f\u60f3\u4e86\u89e3\u4e00\u4e0b\u5728Go \u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u7b80\u5355\u7248\u5b9e\u73b0\uff0c\u6765\u81ea <a href=\"https:\/\/stackoverflow.com\/a\/21508289\">this.lau_<\/a>\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"os\"\r\n    \"time\"\r\n)\r\n\r\nfunc watchFile(filePath string) error {\r\n    initialStat, err := os.Stat(filePath)\r\n    if err != nil {\r\n        return err\r\n    }\r\n\r\n    for {\r\n        stat, err := os.Stat(filePath)\r\n        if err != nil {\r\n            return err\r\n        }\r\n\r\n        if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {\r\n            break\r\n        }\r\n\r\n        time.Sleep(1 * time.Second)\r\n    }\r\n\r\n    return nil\r\n}\r\n\r\nfunc main() {\r\n    doneChan := make(chan bool)\r\n\r\n    go func(doneChan chan bool) {\r\n        defer func() {\r\n            doneChan &lt;- true\r\n        }()\r\n\r\n        err := watchFile(\"\/private\/tmp\/test\")\r\n        if err != nil {\r\n            fmt.Println(err)\r\n        }\r\n\r\n        fmt.Println(\"File has been changed\")\r\n    }(doneChan)\r\n\r\n    &lt;-doneChan\r\n}<\/pre>\n<hr \/>\n<p>\u4f7f\u7528\u00a0fsnotify \u5305\u7684\u5b98\u65b9\u7528\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"github.com\/fsnotify\/fsnotify\"\r\n    \"log\"\r\n)\r\n\r\nfunc ExampleNewWatcher() {\r\n    watcher, err := fsnotify.NewWatcher()\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n    defer watcher.Close()\r\n\r\n    done := make(chan bool)\r\n    go func() {\r\n        for {\r\n            select {\r\n            case event, ok := &lt;-watcher.Events:\r\n                if !ok {\r\n                    return\r\n                }\r\n                log.Println(\"event:\", event)\r\n                if event.Op&amp;fsnotify.Write == fsnotify.Write {\r\n                    log.Println(\"modified file:\", event.Name)\r\n                }\r\n            case err, ok := &lt;-watcher.Errors:\r\n                if !ok {\r\n                    return\r\n                }\r\n                log.Println(\"error:\", err)\r\n            }\r\n        }\r\n    }()\r\n\r\n    err = watcher.Add(\"\/private\/tmp\/foo\")\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n    &lt;-done\r\n}\r\n\r\nfunc main() {\r\n    ExampleNewWatcher()\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/8270441\/go-language-how-detect-file-changing\">Go language how detect file changing?<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/fsnotify\/fsnotify\">Cross-platform file system notifications for Go.<\/a><br \/>\n<a href=\"https:\/\/github.com\/fsnotify\/fsnotify\/blob\/master\/example_test.go\">https:\/\/github.com\/fsnotify\/fsnotify\/blob\/master\/example_test.go<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/os\/#FileInfo\">https:\/\/golang.org\/pkg\/os\/#FileInfo<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/os\/#File.Seek\">https:\/\/golang.org\/pkg\/os\/#File.Seek<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4e4b\u524d\u5728Linux\u4e0b\u4f7f\u7528\u8fc7inotify-tools\u6765\u83b7\u77e5\u6587\u4ef6\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u5728\u535a\u5ba2\u91cc\u4e5f\u8bb0\u5f55 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1251,564,620],"class_list":["post-4166","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-fsnotify","tag-golang","tag-inotify"],"views":3010,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4166"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4166\/revisions"}],"predecessor-version":[{"id":4167,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4166\/revisions\/4167"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}