{"id":10008327,"date":"2023-09-25T19:54:56","date_gmt":"2023-09-25T11:54:56","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008327"},"modified":"2023-09-25T19:54:56","modified_gmt":"2023-09-25T11:54:56","slug":"%e6%8e%8c%e6%8f%a1-go-%e7%9a%84%e8%ae%a1%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008327","title":{"rendered":"\u638c\u63e1 Go \u7684\u8ba1\u65f6\u5668"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>\u5b9a\u65f6\u5668\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u5b89\u6392\u4efb\u52a1\u6216\u6267\u884c\u4ee3\u7801\u3002\u5728 Go \u4e2d\uff0c\u5b9a\u65f6\u5668\u662f\u901a\u8fc7 <code>time<\/code> \u5305\u5b9e\u73b0\u7684\uff0c\u8be5\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u529f\u80fd\u6765\u521b\u5efa\u3001\u542f\u52a8\u3001\u505c\u6b62\u548c\u6709\u6548\u5904\u7406\u5b9a\u65f6\u5668\u3002\u6211\u4eec\u5c06\u63a2\u7d22 Go \u4e2d\u5b9a\u65f6\u5668\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5b9a\u65f6\u5668\u3002<\/p>\n<h2>\u521b\u5efa\u8ba1\u65f6\u5668<\/h2>\n<p>\u8981\u5728 Go \u4e2d\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>time.NewTimer()<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u6301\u7eed\u65f6\u95f4\u4f5c\u4e3a\u53c2\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"go\">func CreateTimer() {\n    timer := time.NewTimer(2 * time.Second)\n    fmt.Println(&quot;Timer created.&quot;)\n\n    &lt;-timer.C \/\/ \u963b\u585e\n    fmt.Println(&quot;Timer expired.&quot;)\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>time.NewTimer()<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u6301\u7eed\u65f6\u95f4\u4e3a 2 \u79d2\u7684\u65b0\u5b9a\u65f6\u5668\u3002<code>&lt;-timer.C<\/code> \u8bed\u53e5\u4f1a\u963b\u585e\u6267\u884c\uff0c\u76f4\u5230\u5b9a\u65f6\u5668\u8fc7\u671f\u3002\u5b9a\u65f6\u5668\u5230\u671f\u540e\uff0c&#8221;Timer expired.&#8221;\uff08\u5b9a\u65f6\u5668\u5df2\u8fc7\u671f\uff09\u4fe1\u606f\u5c06\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<h2>\u505c\u6b62\u8ba1\u65f6\u5668<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u60f3\u5728\u5b9a\u65f6\u5668\u5230\u671f\u524d\u505c\u6b62\u5b83\u3002\u4e3a\u6b64\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b9a\u65f6\u5668\u5bf9\u8c61\u7684 <code>Stop()<\/code> \u65b9\u6cd5\u3002\u8ba9\u6211\u4eec\u4fee\u6539\u4e4b\u524d\u7684\u793a\u4f8b\uff0c\u52a0\u5165\u5b9a\u65f6\u5668\u505c\u6b62\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"go\">func StopTimer() {\n    timer := time.NewTimer(2 * time.Second)\n    fmt.Println(&quot;Timer created.&quot;)\n\n    go func() {\n        &lt;-timer.C\n        fmt.Println(&quot;Timer expired.&quot;)\n    }()\n\n    time.Sleep(1 * time.Second)\n    stopped := timer.Stop()\n    if stopped {\n        fmt.Println(&quot;Timer stopped.&quot;)\n    } else {\n        fmt.Println(&quot;Timer has already expired.&quot;)\n    }\n}<\/code><\/pre>\n<p>\u5728\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a goroutine \u6765\u5904\u7406\u5b9a\u65f6\u5668\u8fc7\u671f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u5b9a\u65f6\u5668\u8fc7\u671f\u524d\u505c\u6b62\u5b83\u3002\u6211\u4eec\u4f7f\u7528 <code>time.Sleep()<\/code> \u51fd\u6570\u6765\u6a21\u62df\u5728\u5c1d\u8bd5\u505c\u6b62\u8ba1\u65f6\u5668\u4e4b\u524d\u6b63\u5728\u8fdb\u884c\u7684\u4e00\u4e9b\u5de5\u4f5c\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528 <code>timer.Stop()<\/code> \u505c\u6b62\u5b9a\u65f6\u5668\u3002\u5982\u679c\u5b9a\u65f6\u5668\u5df2\u8fc7\u671f\uff0c<code>timer.Stop()<\/code> \u8fd4\u56de false\uff0c\u5e76\u6253\u5370 &#8220;\u5b9a\u65f6\u5668\u5df2\u8fc7\u671f&#8221;\u3002\u5426\u5219\uff0c\u6211\u4eec\u5c06\u6253\u5370 &#8220;\u5b9a\u65f6\u5668\u5df2\u505c\u6b62&#8221;\u3002<\/p>\n<h2>\u91cd\u7f6e\u8ba1\u65f6\u5668<\/h2>\n<p>Go \u8fd8\u63d0\u4f9b\u4e86\u91cd\u7f6e\u6d3b\u52a8\u5b9a\u65f6\u5668\u7684\u65b9\u6cd5\u3002\u901a\u8fc7 <code>Reset()<\/code> \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u6539\u6d3b\u52a8\u5b9a\u65f6\u5668\u7684\u6301\u7eed\u65f6\u95f4\uff0c\u91cd\u65b0\u5f00\u59cb\u5012\u8ba1\u65f6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"go\">func ResetTimer() {\n    timer := time.NewTimer(10 * time.Second)\n    fmt.Printf(&quot;time: %d, Timer created.\/n&quot;, time.Now().Unix())\n\n    time.Sleep(2 * time.Second)\n    reset := timer.Reset(3 * time.Second)\n    if reset {\n        fmt.Printf(&quot;time: %d, Timer reset.\/n&quot;, time.Now().Unix())\n    } else {\n        fmt.Printf(&quot;time: %d, Timer has already expired.\/n&quot;, time.Now().Unix())\n    }\n    &lt;-timer.C \/\/ \u963b\u585e\n    fmt.Printf(&quot;time: %d, Timer expired again.\/n&quot;, time.Now().Unix())\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<pre><code class=\"go\">time: 1695183503, Timer created.\ntime: 1695183505, Timer reset.\ntime: 1695183508, Timer expired again.<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6301\u7eed\u65f6\u95f4\u4e3a 10 \u79d2\u7684\u8ba1\u65f6\u5668\u3002\u4f7f\u7528 <code>time.Sleep()<\/code> \u7b49\u5f85 2 \u79d2\u540e\uff0c\u6211\u4eec\u8c03\u7528 <code>timer.Reset()<\/code>\uff0c\u65b0\u7684\u6301\u7eed\u65f6\u95f4\u4e3a 3 \u79d2\u3002\u5982\u679c\u5b9a\u65f6\u5668\u5c1a\u672a\u8fc7\u671f\uff0c\u5219\u91cd\u7f6e\u64cd\u4f5c\u6210\u529f\uff0c\u6211\u4eec\u5c06\u6253\u5370 &#8220;\u5b9a\u65f6\u5668\u91cd\u7f6e&#8221;\u3002\u5426\u5219\uff0c\u8fdb\u5165\u5230 <code>&lt;-timer.C<\/code> \u963b\u585e\u9636\u6bb5\uff0c\u7136\u540e\u6253\u5370 \u6211\u4eec\u5c06\u6253\u5370 &#8220;Timer expired again.&#8221;\u3002<\/p>\n<h2>\u91cd\u7f6e\u5b9a\u65f6\u5668\u4e0e\u505c\u6b62\u5b9a\u65f6\u5668<\/h2>\n<p>\u4e86\u89e3\u91cd\u7f6e\u5b9a\u65f6\u5668\u548c\u4f7f\u7528 <code>Stop()<\/code> \u505c\u6b62\u5b9a\u65f6\u5668\u4e4b\u95f4\u7684\u533a\u522b\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code class=\"go\">func CompareResetAndStop() {\n    timer := time.NewTimer(5 * time.Second)\n    fmt.Printf(&quot;time: %d, Timer created.\/n&quot;, time.Now().Unix())\n\n    go func() {\n        &lt;-timer.C\n        fmt.Printf(&quot;time: %d, Timer expired.\/n&quot;, time.Now().Unix())\n    }()\n\n    time.Sleep(2 * time.Second)\n    timer.Reset(3 * time.Second)\n    fmt.Printf(&quot;time: %d, Timer reset.\/n&quot;, time.Now().Unix())\n\n    time.Sleep(2 * time.Second)\n    timer.Stop()\n    fmt.Printf(&quot;time: %d, Timer stopped.\/n&quot;, time.Now().Unix())\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<pre><code class=\"go\">time: 1695183802, Timer created.\ntime: 1695183804, Timer reset.\ntime: 1695183806, Timer stopped.<\/code><\/pre>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6301\u7eed\u65f6\u95f4\u4e3a 5 \u79d2\u7684\u8ba1\u65f6\u5668\u30022 \u79d2\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>timer.Reset()<\/code> \u5c06\u8ba1\u65f6\u5668\u91cd\u7f6e\u4e3a 3 \u79d2\u3002\u4e4b\u540e\uff0c\u518d\u8fc7 2 \u79d2\uff0c\u6211\u4eec\u4f7f\u7528 <code>timer.Stop()<\/code> \u505c\u6b62\u8ba1\u65f6\u5668\u3002\u91cd\u7f6e\u5b9a\u65f6\u5668\u4f1a\u6539\u53d8\u5176\u6301\u7eed\u65f6\u95f4\u5e76\u91cd\u65b0\u5f00\u59cb\u5012\u8ba1\u65f6\uff0c\u800c\u505c\u6b62\u5b9a\u65f6\u5668\u5219\u4f1a\u7acb\u5373\u505c\u6b62\u6267\u884c\uff0c\u65e0\u8bba\u5269\u4f59\u6301\u7eed\u65f6\u95f4\u591a\u957f\u3002<\/p>\n<h2>\u5e26 Ticker \u7684\u8ba1\u65f6\u5668<\/h2>\n<p>Go \u63d0\u4f9b\u4e86\u4e00\u79cd <code>Ticker<\/code> \u7c7b\u578b\uff0c\u5b83\u662f\u4e00\u79cd\u4e13\u95e8\u7684\u5b9a\u65f6\u5668\uff0c\u53ef\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u91cd\u590d\u89e6\u53d1\u3002\u5b9a\u65f6\u5668\u53ef\u7528\u4e8e\u5b9a\u671f\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"go\">func Tick() {\n    ticker := time.NewTicker(1 * time.Second)\n    defer ticker.Stop()\n\n    go func() {\n        for range ticker.C {\n            fmt.Printf(&quot;time: %d, Ticker ticked!\/n&quot;, time.Now().Unix())\n        }\n    }()\n\n    time.Sleep(5 * time.Second)\n}<\/code><\/pre>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>time.NewTicker()<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u6301\u7eed\u65f6\u95f4\u4e3a 1 \u79d2\u7684 Ticker\u3002\u7136\u540e\uff0c\u6211\u4eec\u542f\u52a8\u4e00\u4e2a goroutine\uff0c\u4ece <code>ticker.C<\/code> channel \u63a5\u6536\u503c\uff0c\u6bcf\u5f53\u6ef4\u7b54\u58f0\u54cd\u8d77\u65f6\uff0cgoroutine \u5c31\u4f1a\u53d1\u51fa\u4e00\u4e2a\u503c\u3002\u5728 goroutine \u4e2d\uff0c\u6bcf\u6b21\u63a5\u6536\u5230\u4e00\u4e2a tick \u65f6\uff0c\u6211\u4eec\u90fd\u4f1a\u6253\u5370 &#8220;Ticker ticked!&#8221;\u3002\u8c03\u7528 <code>time.Sleep()<\/code> \u53ef\u4ee5\u8ba9\u6ef4\u7b54\u6ef4\u7b54\u8fd0\u884c 5 \u79d2\u949f\uff0c\u7136\u540e\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<h2>\u4f7f\u7528 Select \u7684\u8d85\u65f6<\/h2>\n<p>Go \u7684 <code>select<\/code> \u8bed\u53e5\u5141\u8bb8\u5728\u591a\u4e2a\u901a\u9053\u4e0a\u6267\u884c\u975e\u963b\u585e\u64cd\u4f5c\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u4f7f\u7528\u8ba1\u65f6\u5668\u5b9e\u73b0\u8d85\u65f6\u3002<\/p>\n<pre><code class=\"go\">func TimeOut() {\n    ch := make(chan string)\n\n    go func() {\n        time.Sleep(2 * time.Second)\n        ch &lt;- &quot;Operation completed.&quot;\n    }()\n\n    select {\n    case msg := &lt;-ch:\n        fmt.Println(msg)\n    case &lt;-time.After(1 * time.Second):\n        fmt.Println(&quot;Timeout reached.&quot;)\n    }\n}<\/code><\/pre>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a <code>channel ch<\/code>\uff0c\u5e76\u542f\u52a8\u4e00\u4e2a goroutine \u6765\u6a21\u62df\u8017\u65f6 2 \u79d2\u7684\u64cd\u4f5c\u3002\u6211\u4eec\u4f7f\u7528 <code>select<\/code> \u8bed\u53e5\u4ece <code>ch<\/code> \u63a5\u6536\u4fe1\u606f\uff0c\u6216\u4f7f\u7528 <code>time.After()<\/code> \u7b49\u5f85\u8d85\u65f6\u3002\u5982\u679c\u64cd\u4f5c\u5728 1 \u79d2\u5185\u5b8c\u6210\uff0c\u5219\u6253\u5370\u6d88\u606f\u3002\u5426\u5219\uff0c\u5c06\u6267\u884c\u8d85\u65f6\u60c5\u51b5\uff0c\u5e76\u6253\u5370 &#8220;Timeout reached.&#8221;\u3002<\/p>\n<p>\u672c\u6587\u7531<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=E40ySaEN8jEVEAHfyrtIUA%3D%3D.CVE%2FGn8xxXBCHSqgch0ESj%2FDHbQcd8q%2B6pCIH9lzWUs%3D\" rel=\"noopener noreferrer\">mdnice<\/a>\u591a\u5e73\u53f0\u53d1\u5e03<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044254333\">https:\/\/segmentfault.com\/a\/1190000044254333<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u5b9a\u65f6\u5668\u662f\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u5b89\u6392\u4efb\u52a1\u6216\u6267\u884c\u4ee3\u7801\u3002\u5728 Go \u4e2d\uff0c\u5b9a\u65f6\u5668\u662f\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008327","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008327"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008327\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}