{"id":4164,"date":"2018-11-03T19:37:55","date_gmt":"2018-11-03T11:37:55","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4164"},"modified":"2018-11-03T19:37:55","modified_gmt":"2018-11-03T11:37:55","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a015-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0tail-f%e5%91%bd%e4%bb%a4%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4164.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#15-\u5982\u4f55\u5b9e\u73b0tail -f\u547d\u4ee4\u7684\u529f\u80fd"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u60f3\u7740\u5728\u5b66\u4e60Go \u8bed\u8a00\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c1d\u8bd5\u628a\u5e38\u89c1\u547d\u4ee4\u7684\u529f\u80fd\u7528Go \u8bed\u8a00\u5b9e\u73b0\u4e00\u904d\uff0c\u65e2\u52a0\u5f3a\u4e86Go \u8bed\u8a00\u7684\u5b66\u4e60\uff0c\u53c8\u5145\u5b9e\u4e86\u81ea\u5df1\u7684\u4ee3\u7801\u4ed3\u5e93\uff0c\u4e00\u4e3e\u4e24\u5f97\uff0c\u4f55\u4e50\u800c\u4e0d\u4e3a\uff1f<\/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>\u5927\u4f53\u4e0a\u6765\u8bf4\uff0c\u8981\u7528Go \u8bed\u8a00\uff08\u6216\u662f\u5176\u5b83\u7684\u4ec0\u4e48\u7f16\u7a0b\u8bed\u8a00\uff09\u6765\u5b9e\u73b0 <code>tail -f<\/code>\u00a0\u547d\u4ee4\u7684\u529f\u80fd\uff0c\u5927\u81f4\u65b9\u6cd5\u548c\u903b\u8f91\u4e0d\u5916\u4e4e\uff1a<\/p>\n<ol>\n<li>\u76f4\u63a5\u8c03\u7528 tail -f \u547d\u4ee4\uff0c\u8bfb\u53d6\u547d\u4ee4\u7684\u8f93\u51fa\uff1b<\/li>\n<li>\u7406\u89e3 tail -f \u547d\u4ee4\u7684\u539f\u7406\uff0c\u7167\u7740\u539f\u7406\u624b\u5de5\u5b9e\u73b0\uff1b<\/li>\n<li>\u8c03\u7528\u5df2\u6709\u7684\u6210\u719f\u7684\u7b2c\u4e09\u65b9\u5e93\uff1b<\/li>\n<\/ol>\n<p>\u7b2c\u4e00\u79cd\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u7684\u65b9\u5f0f\u5c31\u4e0d\u5728\u8fd9\u8bf4\u4e86\uff0c\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u8bb0\u5f55\u7684\u901a\u8fc7os.exec\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u7684\u6587\u7ae0\u3002<\/p>\n<p>\u7b2c\u4e8c\u79cd\u662f\u6839\u636e tail -f \u547d\u4ee4\u7684\u539f\u7406\u5b8c\u5168\u624b\u5de5\u5b9e\u73b0\uff0c\u4e2a\u4eba\u89c9\u5f97\u5fc5\u8981\u6027\u4e0d\u5927\uff0c\u4e3b\u8981\u6709\u51e0\u70b9\u539f\u56e0\uff1a1\u3001\u539f\u7406\u672c\u8eab\u5e76\u4e0d\u590d\u6742\uff0c\u4e3b\u8981\u662f\u901a\u8fc7\u5b9a\u671f\u8f6e\u8be2\u6216\u662f\u7cfb\u7edf\u5e95\u5c42API\u7684\u901a\u77e5\u673a\u5236\u83b7\u77e5\u6587\u4ef6\u5185\u5bb9\u53d1\u751f\u53d8\u52a8\uff0c\u7136\u540e\u4ece\u4e0a\u4e00\u6b21\uff08\u6216\u7b2c\u4e00\u6b21\uff09\u8bb0\u5f55\u7684\u6587\u4ef6\u504f\u79fboffset\u5f00\u59cb\uff0c\u4f7f\u7528fseek\u79fb\u52a8\u5230\u53d8\u52a8\u5185\u5bb9\u5904\u8c03\u7528read\/fread\u8fdb\u884c\u8bfb\u53d6\u540e\u6253\u5370\u51fa\u6765\uff1b2\u3001\u867d\u7136\u539f\u7406\u4e0d\u590d\u6742\uff0c\u4f46\u662f\u8981\u60f3\u5b9e\u73b0\u7684\u975e\u5e38\u5b8c\u5907\u4e5f\u662f\u5f88\u56f0\u96be\u7684\uff0c\u9700\u8981\u8003\u8651\u5404\u79cd\u5404\u6837\u7684\u5f02\u5e38\/\u590d\u6742\u60c5\u51b5\uff0c\u81ea\u5df1\u5b9e\u73b0\u6027\u4ef7\u6bd4\u4e0d\u9ad8\uff1b3\u3001\u81ea\u5df1\u5b9e\u73b0\u7684\u4e00\u822c\u4e0d\u4f1a\u6bd4\u6210\u719f\u7684\u7b2c\u4e09\u65b9\u5e93\u6548\u7387\u9ad8\u6216\u662f\u6027\u80fd\u597d\uff0c\u6ca1\u6709\u5b9e\u9645\u4f7f\u7528\u4ef7\u503c\u6216\u662f\u5b9e\u9645\u4f7f\u7528\u4ef7\u503c\u5f88\u4f4e\u3002<\/p>\n<p>\u7b2c\u4e09\u79cd\u5c31\u662f\u8c03\u7528\u5df2\u6709\u7684\u6210\u719f\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd9\u91cc\u4e3b\u8981\u662f\u6307\u00a0<a href=\"http:\/\/github.com\/hpcloud\/tail\">github.com\/hpcloud\/tail<\/a>\u00a0\u8fd9\u4e2a\u3002<\/p>\n<hr \/>\n<p>\u4e0a\u9762\u8bf4\u4e86\u4e00\u5806\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u5f15\u51fa\u8fd9\u4e2a\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u4ecb\u7ecd\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"github.com\/hpcloud\/tail\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    filename := \"\/private\/tmp\/test\"\r\n\r\n    tails, err := tail.TailFile(filename, tail.Config{\r\n        ReOpen: true,\r\n        Follow: true,\r\n        \/\/ Location:  &amp;tail.SeekInfo{Offset: 0, Whence: 2},\r\n        MustExist: false,\r\n        Poll:      true,\r\n    })\r\n    if err != nil {\r\n        fmt.Println(\"tail file err:\", err)\r\n        return\r\n    }\r\n\r\n    var msg *tail.Line\r\n    var ok bool\r\n    for {\r\n        msg, ok = &lt;-tails.Lines\r\n        if !ok {\r\n            fmt.Printf(\"tail file close reopen, filename:%s\\n\", tails.Filename)\r\n            time.Sleep(100 * time.Millisecond)\r\n            continue\r\n        }\r\n        fmt.Printf(\"msg(%T): %v\\n\", msg, msg)\r\n        fmt.Printf(\"msg(%T): %v\\n\", msg.Text, msg.Text)\r\n    }\r\n}<\/pre>\n<p>\u4ee5\u4e0a\u3002<\/p>\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:\/\/blog.csdn.net\/u013210620\/article\/details\/78570668\">golang\u57fa\u7840-tailf\u65e5\u5fd7\u7ec4\u4ef6\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/10135738\/reading-log-files-as-theyre-updated-in-go\">Reading log files as they&#8217;re updated in Go<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/19407502\/how-would-i-read-a-growing-file-in-go\">How would I read a growing file in Go?<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/hpcloud\/tail\">https:\/\/github.com\/hpcloud\/tail<\/a><br \/>\n<a href=\"https:\/\/godoc.org\/github.com\/hpcloud\/tail#Line\">https:\/\/godoc.org\/github.com\/hpcloud\/tail#Line<\/a><\/li>\n<li><a href=\"https:\/\/studygolang.com\/articles\/12362\">golang\u6a21\u4efftail\u547d\u4ee4\uff0c\u663e\u793a\u6587\u4ef6\u672b\u5c3e\u6307\u5b9a\u884c\u6570\u7684\u6587\u4ef6\u5185\u5bb9<\/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 \u60f3\u7740\u5728\u5b66\u4e60Go \u8bed\u8a00\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c1d\u8bd5\u628a\u5e38\u89c1\u547d\u4ee4\u7684\u529f\u80fd\u7528Go \u8bed\u8a00\u5b9e\u73b0\u4e00\u904d\uff0c\u65e2\u52a0\u5f3a\u4e86Go  [&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":[1250,564,601],"class_list":["post-4164","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-fileinfo","tag-golang","tag-tail"],"views":6759,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4164","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=4164"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4164\/revisions"}],"predecessor-version":[{"id":4165,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4164\/revisions\/4165"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}