{"id":207312,"date":"2025-07-08T11:12:47","date_gmt":"2025-07-08T03:12:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207312\/"},"modified":"2025-07-08T11:12:47","modified_gmt":"2025-07-08T03:12:47","slug":"%e5%9c%a8-windows-%e7%bb%88%e7%ab%af%e4%b8%8a%e4%bc%98%e9%9b%85%e5%9c%b0%e9%80%80%e5%87%ba-golang","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207312\/","title":{"rendered":"\u5728 Windows \u7ec8\u7aef\u4e0a\u4f18\u96c5\u5730\u9000\u51fa golang"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728 Windows \u7ec8\u7aef\u4e0a\u4f18\u96c5\u5730\u9000\u51fa golang<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 17:27:29<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Windows \u7ec8\u7aef\u4e0a\u4f18\u96c5\u5730\u9000\u51fa golang\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728 windows \u4e0a\u65e0\u6cd5\u6b63\u5e38\u9000\u51fa\u6211\u81ea\u5df1\u7684 go \u7a0b\u5e8f\u3002 os.interrupt \u4f3c\u4e4e\u4e5f\u4e0d\u8d77\u4f5c\u7528\u3002\u6211\u60f3\u77e5\u9053\u662f\u5426\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f18\u96c5\u5730\u9000\u51fagolang\u7a0b\u5e8f\uff1f<\/p>\n<pre>func main() {\n\n    sigs := make(chan os.Signal, 1)\n    done := make(chan bool, 1)\n\n    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)\n\n    go func() {\n        sig := &lt;-sigs\n        fmt.Println()\n        fmt.Println(sig)\n        done &lt;- true\n    }()\n\n    fmt.Println(\"awaiting signal\")\n    &lt;-done\n    fmt.Println(\"exiting\")\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u4ee3\u7801\u5de5\u4f5c\u6b63\u5e38\uff0c\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\uff0c\u6211\u5728 windows \u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u6d4b\u8bd5\u4e86\u5b83\u3002<\/p>\n<p>\u4e3a\u4e86\u8ba9\u60a8\u66f4\u52a0\u4fe1\u670d\uff0c\u8bf7\u5c1d\u8bd5\u5728 <code>&lt;-done<\/code> \u4e4b\u540e\u6dfb\u52a0 <code>time.sleep(5 * time.second)<\/code>\uff0c\u7136\u540e\u8fdb\u884c\u6d4b\u8bd5\u3002\u5f53\u4f60\u4e2d\u65ad\u5b83\u65f6\uff0c\u7a0b\u5e8f\u53ea\u4f1a\u57285\u79d2\u540e\u5173\u95ed\u3002<\/p>\n<pre>fmt.println(\"awaiting signal\")\n&lt;-done\n\nfmt.println(\"exiting in 5 seconds\")\ntime.sleep(5 * time.second)\nfmt.println(\"exit success\")\n<\/pre>\n<p>\u4e5f\u50cf@ain\u5728\u8bc4\u8bba\u4e2d\u6240\u8bf4\u7684\u90a3\u6837\uff0c\u5c1d\u8bd5\u5728<code>signal.notify<\/code>\u4e0a\u6dfb\u52a0<code>os.interrupt<\/code><\/p>\n<pre>signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)\n<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5728 Windows \u7ec8\u7aef\u4e0a\u4f18\u96c5\u5730\u9000\u51fa golang\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207312","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207312"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207312\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}