{"id":275192,"date":"2021-01-12T21:34:04","date_gmt":"2021-01-12T13:34:04","guid":{"rendered":"http:\/\/4563.org\/?p=275192"},"modified":"2021-01-12T21:34:04","modified_gmt":"2021-01-12T13:34:04","slug":"%e3%80%8c%e8%af%91%e3%80%8dgolang-%e4%b8%ad%e7%9a%84-init-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=275192","title":{"rendered":"\u300c\u8bd1\u300dGolang \u4e2d\u7684 init \u51fd\u6570"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u300c\u8bd1\u300dGolang \u4e2d\u7684 init \u51fd\u6570               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : darluc <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u300c\u9605\u8bfb\u5168\u6587\u300d<\/p>\n<p>\u6807\u8bc6\u7b26 <code>main<\/code> \u65e0\u6240\u4e0d\u5728\u3002\u6bcf\u4e2a Go \u7a0b\u5e8f\u7684\u6267\u884c\u90fd\u662f\u4ece <code>main<\/code> \u5305\u4e2d\u4e00\u4e2a\u62e5\u6709\u76f8\u540c\u540d\u5b57\u7684\u51fd\u6570\u5f00\u59cb\u7684\u3002\u5f53\u8fd9\u4e2a <code>main<\/code> \u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u6574\u4e2a\u7a0b\u5e8f\u4e5f\u9000\u51fa\u4e86\u6267\u884c\u3002<code>init<\/code> \u51fd\u6570\u4e5f\u626e\u6f14\u7740\u7279\u5b9a\u7684\u89d2\u8272\uff0c\u672c\u6587\u4f1a\u63cf\u8ff0\u5b83\u4eec\u7684\u7279\u6027\u5e76\u4ecb\u7ecd\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p><code>init<\/code> \u51fd\u6570\u662f\u5b9a\u4e49\u5728\u5305\u7ea7\u522b\u7684\uff0c\u5b83\u88ab\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u65e0\u6cd5\u4f7f\u7528\u8868\u8fbe\u5f0f\u521d\u59cb\u5316\u7684\u53d8\u91cf<\/li>\n<li>\u68c0\u67e5\u548c\u4fee\u590d\u7a0b\u5e8f\u7684\u72b6\u6001<\/li>\n<li>\u6ce8\u518c<\/li>\n<li>\u6267\u884c\u4e00\u6b21\u6027\u7684\u8fd0\u7b97<\/li>\n<li>\u4ee5\u53ca\u5176\u5b83<\/li>\n<\/ul>\n<p>\u9664\u4e86\u4e0b\u9762\u8981\u4ecb\u7ecd\u4e00\u4e9b\u533a\u522b\uff0c\u4f60\u53ef\u4ee5\u5c06\u4efb\u4f55\u5728\u4e00\u822c\u51fd\u6570\u4e2d\u6709\u6548\u7684\u4ee3\u7801\u653e\u5728\u5176\u4e2d\u3002<\/p>\n<h2>\u5305\u7684\u521d\u59cb\u5316<\/h2>\n<p>\u8981\u4f7f\u7528\u4e00\u4e2a\u5f15\u5165\u7684\u5305\uff0c\u9996\u5148\u5b83\u9700\u8981\u88ab\u521d\u59cb\u5316\u3002\u8fd9\u662f\u7531 Golang \u7684\u8fd0\u884c\u7cfb\u7edf\u6765\u5b8c\u6210\u7684\uff0c\u7531\u4ee5\u4e0b\u51e0\u6b65\uff08\u987a\u5e8f\u5f88\u91cd\u8981\uff09\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u5f15\u5165\u7684\u5305\uff08\u9012\u5f52\u91ca\u4e49\uff09<\/li>\n<li>\u8ba1\u7b97\u5e76\u521d\u59cb\u5316\u8d4b\u503c\u5305\u7ea7\u522b\u7684\u53d8\u91cf<\/li>\n<li>\u6267\u884c\u5305\u5185\u7684 <code>init<\/code> \u65b9\u6cd5<\/li>\n<\/ol>\n<blockquote>\n<p><em>\u5305\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\uff0c\u5373\u4f7f\u5b83\u88ab\u591a\u6b21\u5f15\u7528<\/em><\/p>\n<\/blockquote>\n<h2>\u987a\u5e8f<\/h2>\n<p>Go \u8bed\u8a00\u7684\u5305\u53ef\u4ee5\u5305\u542b\u8bb8\u591a\u6587\u4ef6\u3002\u90a3\u4e48\u5728\u8fd9\u4e9b\u5305\u548c\u6587\u4ef6\u4e2d\uff0c\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c <code>init<\/code> \u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\u662f\u600e\u6837\u7684\u5462\uff1f\u9996\u5148\uff0c\u521d\u59cb\u5316\u4f9d\u8d56\u673a\u5236\u4f1a\u8d77\u4f5c\u7528\uff08\u8be6\u60c5\u53ef\u4ee5\u67e5\u770b\u201cGo \u4e2d\u7684\u521d\u59cb\u5316\u4f9d\u8d56\u201d\uff09\u3002\u5f53\u4f9d\u8d56\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u5fc5\u987b\u51b3\u5b9a\u5148\u521d\u59cb\u5316 <em>a.go<\/em> \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u8fd8\u662f <em>z.go<\/em> \u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u3002\u8fd9\u4f9d\u8d56\u4e8e\u6587\u4ef6\u5728\u7f16\u8bd1\u5668\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u3002\u5982\u679c <em>z.go<\/em> \u5148\u88ab\u63d0\u4ea4\u7ed9\u6784\u5efa\u7cfb\u7edf\uff0c\u90a3\u4e48\u5b83\u7684\u53d8\u91cf\u5c31\u4f1a\u5148\u4e8e <em>a.go<\/em> \u4e2d\u7684\u53d8\u91cf\u521d\u59cb\u5316\u3002<code>init<\/code> \u65b9\u6cd5\u7684\u8c03\u7528\u4e5f\u9075\u5b88\u76f8\u540c\u7684\u987a\u5e8f\u3002\u8bed\u8a00\u89c4\u683c\u5b9a\u4e49\u4e2d\u5efa\u8bae\u603b\u662f\u91c7\u7528\u76f8\u540c\u7684\u987a\u5e8f\uff0c\u5e76\u4e14\u5c06\u5305\u4e2d\u7684\u6587\u4ef6\u6309\u5355\u8bcd\u62fc\u5199\u987a\u5e8f\u4f20\u5165\uff1a<\/p>\n<blockquote>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u521d\u59cb\u5316\u884c\u4e3a\u53ef\u7a33\u5b9a\u590d\u73b0\uff0c\u6784\u5efa\u7cfb\u7edf\u5e94\u8be5\u503e\u5411\u4e8e\u5c06\u540c\u4e00\u4e2a\u5305\u4e2d\u7684\u591a\u4e2a\u6587\u4ef6\u6309\u6587\u4ef6\u540d\u7684\u5355\u8bcd\u62fc\u5199\u987a\u5e8f\u4f20\u9012\u7ed9\u7f16\u8bd1\u5668\u3002<\/p>\n<\/blockquote>\n<p>\u4e0d\u8fc7\u5bf9\u4e8e\u79fb\u690d\u6027\u8f83\u5dee\u7684\u7a0b\u5e8f\u6765\u8bf4\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7279\u522b\u7684\u987a\u5e8f\u3002\u6211\u4eec\u7528\u4e0b\u9762\u7684\u4f8b\u5b50\u770b\u770b\u8fd9\u4e9b\u662f\u5982\u4f55\u4e00\u8d77\u5de5\u4f5c\u7684\uff1a<\/p>\n<p><strong>sandbox.go<\/strong><\/p>\n<pre><code>package main import \"fmt\" var _ int64 = s() func init() {     fmt.Println(\"init in sandbox.go\") } func s() int64 {     fmt.Println(\"calling s() in sandbox.go\")     return 1 } func main() {     fmt.Println(\"main\") } <\/code><\/pre>\n<p><strong>a.go<\/strong><\/p>\n<pre><code>package main import \"fmt\" var _ int64 = a() func init() {     fmt.Println(\"init in a.go\") } func a() int64 {     fmt.Println(\"calling a() in a.go\")     return 2 } <\/code><\/pre>\n<p><strong>z.go<\/strong><\/p>\n<pre><code>package main import \"fmt\" var _ int64 = z() func init() {     fmt.Println(\"init in z.go\") } func z() int64 {     fmt.Println(\"calling z() in z.go\")     return 3 } <\/code><\/pre>\n<p>\u7a0b\u5e8f\u8f93\u51fa\uff1a<\/p>\n<pre><code>calling a() in a.go calling s() in sandbox.go calling z() in z.go init in a.go init in sandbox.go init in z.go main <\/code><\/pre>\n<h2>\u5c5e\u6027<\/h2>\n<p><code>init<\/code> \u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u4e8e <code>main<\/code> \u76f8\u6bd4\uff0c\u6807\u8bc6\u7b26 <code>init<\/code> \u662f\u6ca1\u6709\u88ab\u7533\u660e\u7684\uff0c\u6240\u4ee5\u65e0\u6cd5\u88ab\u5f15\u7528\uff1a<\/p>\n<p>\u300c\u9605\u8bfb\u5168\u6587\u300d<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4913178\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CEBBCAT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u89c9\u5f97\u6807\u9898\u4e2d\u7684\u8bd1\u53ef\u4ee5\u6216\u8005\u8bf4\u5e94\u8be5\u7528\u65b9\u62ec\u53f7\uff0c\u5c31\u662f\u90a3\u79cd\u5185\u90e8\u6d82\u9ed1\u7684\uff0cV2 \u597d\u50cf\u4f1a\u8f6c\u5316\u90a3\u79cd\u62ec\u53f7\u5230\u8fd9\u4e2a\uff1a [\uff0c\u4e5f\u4e0d\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4913179\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iugo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/golang.org\/doc\/effective_go.html#init                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4913180\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : undertonememorie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5355\u4e2a\u6587\u4ef6\u591a\u4e2a init \u6267\u884c\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f\u540c\u4e00 package \u4e0b\uff0cinit \u7684\u6267\u884c\u987a\u5e8f\u53c8\u662f\u4ec0\u4e48\uff0c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u300c\u8bd1\u300dGolang \u4e2d\u7684 init&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\/275192"}],"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=275192"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/275192\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}