{"id":126039,"date":"2020-01-13T07:34:20","date_gmt":"2020-01-12T23:34:20","guid":{"rendered":"http:\/\/4563.org\/?p=126039"},"modified":"2020-01-13T07:34:20","modified_gmt":"2020-01-12T23:34:20","slug":"%e5%9c%a8-go-%e5%ae%98%e6%96%b9%e6%96%87%e6%a1%a3%e4%b8%ad%e7%9c%8b%e5%88%b0%e7%9a%84%e4%b8%80%e5%8f%a5%e8%af%9d","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=126039","title":{"rendered":"\u5728 go \u5b98\u65b9\u6587\u6863\u4e2d\u770b\u5230\u7684\u4e00\u53e5\u8bdd"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5728 go \u5b98\u65b9\u6587\u6863\u4e2d\u770b\u5230\u7684\u4e00\u53e5\u8bdd               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Akiyu <\/span>  <span><i><\/i> 54<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u539f\u5730\u5740 : https:\/\/golang.org\/ref\/mem<br \/> \u5728\u5176\u4e2d\u7684 advice \u4e2d :<\/p>\n<pre><code>If you must read the rest of this document to understand the behavior of your program, you are being too clever. Don't be clever. <\/code><\/pre>\n<p>\u8fd9\u4e2a\u662f\u5565\u610f\u601d\u5462?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"54816\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AndrewGhost <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Don\u2019t be clever\uff0c \u4e0d\u8981\u81ea\u4f5c\u806a\u660e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54817\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gstqc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             tl, dr \u7684\u52a0\u5f3a\u7248                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54818\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8003 4 \u7ea7\u65f6\u5019\u9047\u5230\u8fc7<\/p>\n<p>It&#8217;s not a good thing to be too clever. <\/p>\n<p>\u4e0d\u8fc7\u4f3c\u4e4e\u8bed\u5883\u4e0d\u4e00\u6837, \u5e2e\u9876                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54819\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u70b9\u5230\u6587\u6863\u770b\u4e86\u4e0b, \u6ca1\u6709\u5176\u4ed6\u610f\u601d, \u5c31\u662f\u4e0d\u5efa\u8bae\u770b\u5269\u4e0b\u7684\u6587\u6863, \u65b0\u624b\u4e0d\u8981\u8f7b\u6613\u5c1d\u8bd5 memory operations, \u7528\u73b0\u6210\u7684 channel \u6216\u8005 sync                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54820\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shintendo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u89c9\u5f97 too clever \u4e0d\u662f\u8bf4\u4f60\u8bfb\u6587\u6863\uff0c\u800c\u662f\u8bf4\u4f60\u7684\u7a0b\u5e8f\u3002<br \/>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u8bfb\u5b8c\u4e0b\u9762\u7684\u6587\u6863\u624d\u80fd\u7406\u89e3\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5199\u5f97 too clever \u4e86\uff08\u7c7b\u4f3c\u5947\u6280\u6deb\u5de7\u7684\u610f\u601d\uff1f\uff09\u3002<br \/>\u6ca1\u5b66\u8fc7 go\uff0c\u7eaf\u731c\u60f3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54821\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : orzorzorzorz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u4e0a\u53bb\u50cf\u662f\u5632\u8bbd\u554a&#8230;\u522b\u770b\uff0c\u770b\u5c31\u79c3\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54822\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Akiyu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u6709\u4e09\u79cd\u542b\u4e49:<br \/>1. \u6bd4\u8f83\u5632\u8bbd\u7684\u90a3\u79cd, \u5982\u679c\u628a\u6240\u6709\u6587\u6863\u770b\u5b8c\u624d\u660e\u767d, \u90a3\u5c31&#8221;\u592a\u806a\u660e&#8221;\u4e86. <br \/>\u4e0d\u8fc7\u4e00\u4e2a\u5b98\u65b9\u6587\u6863\u5e94\u8be5\u4e0d\u4f1a\u8fd9\u4e48\u505a<\/p>\n<p>2. \u4f5c\u8005\u4e0d\u592a\u5efa\u8bae\u4e00\u5b9a\u8981\u770b\u5b8c\u6240\u6709, \u7c7b\u4f3c\u4e8e &#8220;\u597d\u8bfb\u4e66\u4e0d\u6c42\u751a\u89e3&#8221;, \u7406\u89e3\u4e3a\u91cd, \u4e0d\u8981\u5168\u90e8\u770b\u5b8c<br \/>\u5c31\u662f\u5efa\u8bae\u4e0d\u8981\u592a\u6b7b\u677f, \u81ea\u4f5c\u806a\u660e<\/p>\n<p>3. \u5c31\u662f @ClericPy \u8bf4\u7684, \u65b0\u624b\u8001\u8001\u5b9e\u5b9e\u5c31\u884c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54823\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lhx2008 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u624b\u673a\u6253\u4e0d\u5f00\uff0c\u6211\u731c\u662f\u7406\u89e3\u62bd\u8c61\u7684\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\uff0c\u6ca1\u6709\u5fc5\u8981\u7528\u5e95\u5c42\u5b9e\u73b0\u7684\u4e1c\u897f\u3002\u8fd9\u4e5f\u6ca1\u6709\u9519\uff0c\u4f60\u4f1a\u5199\uff0c\u4f60\u7684\u540c\u4e8b\u4e0d\u4f1a\uff0c\u534f\u4f5c\u5c31\u6210\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54824\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dbw9580 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6587\u6863\u662f\u5173\u4e8e go routine \u4e0d\u505a\u540c\u6b65\u4e5f\u4e00\u5b9a\u80fd\u591f\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168\u7684\u51e0\u4e2a\u6761\u4ef6\u3002<br \/>\u4e0b\u9762\u51e0\u53e5\u8bdd\u53eb\u4f60\u7528 channel\/sync atomic \u505a\u663e\u5f0f\u540c\u6b65\uff0c\u800c\u4e0d\u8981\u4f9d\u8d56\u8fd9\u4e2a\u6587\u6863\u91cc\u63cf\u8ff0\u7684\u6761\u4ef6\u800c\u4e0d\u505a\u540c\u6b65\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54825\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : WilsonGGG <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9e\u8df5\u6307\u5bfc\u7406\u8bba\uff0c\u5148\u5e72\u4e86\u518d\u8bf4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54826\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : catror <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7ffb\u8bd1\uff1a<br \/>\u5982\u679c\u4f60\u4e00\u5b9a\u8981\u8bfb\u8fd9\u7bc7\u6587\u6863\u624d\u80fd\u786e\u5b9a\u4f60\u7684\u7a0b\u5e8f\u884c\u4e3a\uff0c\u90a3\u4e48\u4f60\u4e00\u5b9a\u662f\u81ea\u4f5c\u806a\u660e\u4e86\uff08\u9690\u542b\u610f\uff1a\u7a0b\u5e8f\u884c\u4e3a\u4f9d\u8d56\u4e8e\u8fd9\u7bc7\u6587\u6863\u4e2d\u63cf\u8ff0\u7684\u884c\u4e3a\uff0c\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a\uff09\u3002<br \/>\u522b\u81ea\u4f5c\u806a\u660e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54827\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lance6716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u7b80\u5355\u3001\u660e\u663e\u7684 sync\uff0c\u4e0d\u8981\u4e86\u89e3 go \u5185\u90e8\u7684\u8fd9\u4e9b\u96be\u61c2\u96be\u7528\u7684 happens before                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"54828\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9e\u9645\u4e0a\u662f\u8bf4,\u4e0d\u8981\u5c1d\u8bd5\u901a\u8fc7\u8fc7\u4e8e\u5e95\u5c42\u7684 api \u53bb\u64cd\u4f5c\u5185\u5b58.<\/p>\n<p>Advice<br \/>Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access.<\/p>\n<p>To serialize access, protect the data with channel operations or other synchronization primitives such as those in the sync and sync\/atomic packages.<\/p>\n<p>If you must read the rest of this document to understand the behavior of your program, you are being too clever.<\/p>\n<p>Don&#8217;t be clever.<\/p>\n<p>\u5fe0\u544a<\/p>\n<p>\u4fee\u6539\u7531\u591a\u4e2a goroutine \u540c\u65f6\u8bbf\u95ee\u7684\u6570\u636e\u7684\u7a0b\u5e8f\u5fc5\u987b\u5e8f\u5217\u5316\u6b64\u7c7b\u8bbf\u95ee\u3002<\/p>\n<p>\u8981\u5e8f\u5217\u5316\u8bbf\u95ee\uff0c\u8bf7\u4f7f\u7528\u901a\u9053\u64cd\u4f5c\u6216\u5176\u4ed6\u540c\u6b65\u539f\u8bed\uff08\u4f8b\u5982 sync \u548c sync \/ atomic \u5305\u4e2d\u7684\u539f\u8bed\uff09\u4fdd\u62a4\u6570\u636e\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5fc5\u987b\u9605\u8bfb\u672c\u6587\u6863\u7684\u5176\u4f59\u90e8\u5206\u4ee5\u4e86\u89e3\u7a0b\u5e8f\u7684\u884c\u4e3a\uff0c\u90a3\u4e48\u60a8\u5c31\u592a\u806a\u660e\u4e86\u3002<\/p>\n<p>\u522b\u806a\u660e                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 go \u5b98\u65b9\u6587\u6863\u4e2d\u770b\u5230\u7684\u4e00\u53e5\u8bdd &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/126039"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126039"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/126039\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126039"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}