{"id":207659,"date":"2025-07-08T13:10:22","date_gmt":"2025-07-08T05:10:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207659\/"},"modified":"2025-07-08T13:10:22","modified_gmt":"2025-07-08T05:10:22","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e4%bb%a5%e6%83%b0%e6%80%a7%e6%96%b9%e5%bc%8f%e5%88%9d%e5%a7%8b%e5%8c%96%e5%8c%85%e7%9a%84%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207659\/","title":{"rendered":"\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4ee5\u60f0\u6027\u65b9\u5f0f\u521d\u59cb\u5316\u5305\u7684\u5168\u5c40\u53d8\u91cf\uff1f"},"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>\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4ee5\u60f0\u6027\u65b9\u5f0f\u521d\u59cb\u5316\u5305\u7684\u5168\u5c40\u53d8\u91cf\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 12:57:28<\/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\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4ee5\u60f0\u6027\u65b9\u5f0f\u521d\u59cb\u5316\u5305\u7684\u5168\u5c40\u53d8\u91cf\uff1f\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\u7684\u9879\u76ee\u4e2d\u6709\u591a\u4e2a\u5305\uff1a\u4e3b\u8981\u5305\u548c\u6b21\u8981\u5305\u3002\u4e3b\u8981\u90e8\u5206\u63a5\u53d7\u7528\u6237\u7684\u8f93\u5165\u5e76\u521d\u59cb\u5316\u4e00\u4e9b\u53d8\u91cf\u3002 \u8f85\u52a9\u5305\u5305\u542b\u4e00\u4e9b\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u53ea\u80fd\u5728\u201c\u4e3b\u201d\u5305\u521d\u59cb\u5316\u540e\u624d\u80fd\u521d\u59cb\u5316\uff0c\u800c\u4e0d\u80fd\u66f4\u65e9\u3002<\/p>\n<p>\u6211\u77e5\u9053\u5305\u7684\u51fd\u6570\u201cinit()\u201d\uff0c\u4f46\u8fd9\u5728\u6211\u7684\u60c5\u51b5\u4e0b\u4e0d\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u4ee5\u60f0\u6027\u65b9\u5f0f\u521d\u59cb\u5316\u6570\u636e\uff0c\u5e76\u4e14\u65e0\u6cd5\u4f7f\u7528\u6765\u81ea\u5916\u90e8\u7684\u4e00\u4e9b\u53c2\u6570\u6216\u6570\u636e\u3002\u800c\u8fd9\u6b63\u662f\u6211\u60f3\u8981\u7684\u3002<\/p>\n<pre>\/\/main package\n\n    var (\n        a1 int\n        a2 float\n        a3 string\n        a4 MyStruct\n        a5 MyStruct2\n    )\n\n    func main() {\n        \/\/process user input\n        \/\/...........\n        \/\/some other stuff\n\n\n        \/\/ I want that by this point, the global variables of package2 to have been initialized\n        \/\/ **based on** the processed user input from above\n        \/\/namely, before the first access to the package\n        package2.Func1()\n\n        \/\/ I want that by this point, the global variables of package3 to have been initialized\n        \/\/ **based on** the processed user input from above\n        \/\/namely, before the first access to the package\n        package3.Func11()\n\n    }<\/pre>\n<h2>\u8fd9\u4e2a\u95ee\u9898\u6709\u89e3\u51b3\u529e\u6cd5\u5417\uff1f<\/h2>\n<p>\u6211\u53ef\u4ee5\u5728\u5305 package2 \u548c package3 \u4e2d\u6709\u4e00\u4e2a\u51fd\u6570\u201cinitdata()\u201d\uff0c\u4f46\u662f\u6211\u5982\u4f55\u786e\u4fdd\u5728\u8c03\u7528\u4efb\u4f55\u51fd\u6570\u4e4b\u524d\u4e0d\u4f1a\u5fd8\u8bb0\u4ee5\u5f3a\u5236\u65b9\u5f0f\u5c06\u5176\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u5305\u7684\u5176\u4ed6\u529f\u80fd\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4f60\u5e94\u8be5\u5728 main.go \u4e2d\u4f7f\u7528 init \u51fd\u6570<\/p>\n<pre>\nfunc init(){\n    \/\/ get user input and set variables\n\n    \/\/ call package 2 and 3 after setting the variables in main\n    package2.Func1()\n    package3.Func1()\n\n}\n\nfunc main(){\n    \/\/ everything is setup properly when code in this main function is launched\n}\n<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4ee5\u60f0\u6027\u65b9\u5f0f\u521d\u59cb\u5316\u5305\u7684\u5168\u5c40\u53d8\u91cf\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-207659","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207659","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=207659"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207659\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}