{"id":184448,"date":"2024-05-08T22:09:32","date_gmt":"2024-05-08T14:09:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/184448.html"},"modified":"2024-05-08T22:09:44","modified_gmt":"2024-05-08T14:09:44","slug":"go%e8%af%ad%e8%a8%80%e5%8f%98%e9%87%8f%e5%a3%b0%e6%98%8evar%e5%92%8c%e4%b8%a4%e7%a7%8d%e6%96%b9%e5%bc%8f%e6%98%af%e5%90%a6%e6%9c%89%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/184448.html","title":{"rendered":"Go\u8bed\u8a00\u53d8\u91cf\u58f0\u660evar\u548c:=\u4e24\u79cd\u65b9\u5f0f\u662f\u5426\u6709\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27075634\/85cdcf27-d906-4658-a4e6-927d942036ec.webp\" alt=\"Go\u8bed\u8a00\u53d8\u91cf\u58f0\u660evar\u548c:=\u4e24\u79cd\u65b9\u5f0f\u662f\u5426\u6709\u533a\u522b\" \/><\/p>\n<p><p>Go\u8bed\u8a00\u4e2d\uff0c\u53d8\u91cf\u58f0\u660e\u7684\u4e24\u79cd\u65b9\u5f0f<code>var<\/code>\u548c<code>:=<\/code>\u786e\u5b9e\u5b58\u5728\u533a\u522b\uff1a<strong><code>var<\/code>\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u3001\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3001\u6307\u6d3e\u521d\u59cb\u503c\u6216\u4f7f\u7528\u96f6\u503c\u521d\u59cb\u5316<\/strong>\uff1b\u800c<code>:=<\/code>\u662f\u77ed\u53d8\u91cf\u58f0\u660e\u65b9\u5f0f\uff0c\u4ec5\u7528\u5728\u51fd\u6570\u5185\u90e8\uff0c\u7528\u4e8e\u58f0\u660e\u5e76\u521d\u59cb\u5316\u5c40\u90e8\u53d8\u91cf\uff0c\u53d8\u91cf\u7c7b\u578b\u7531\u53f3\u503c\u63a8\u5bfc\u5f97\u51fa\u3002<code>var<\/code>\u53ef\u4ee5\u5728\u51fd\u6570\u5916\u90e8\u4f7f\u7528\uff0c\u5e76\u5141\u8bb8\u5305\u5185\u6240\u6709\u51fd\u6570\u8bbf\u95ee\uff0c\u800c<code>:=<\/code>\u4ec5\u9650\u4e8e\u51fd\u6570\u5185\u90e8\u4f5c\u7528\u57df\uff0c\u4e14\u4e0d\u80fd\u7528\u4e8e\u5df2\u7ecf\u58f0\u660e\u8fc7\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001VAR \u58f0\u660e\u65b9\u5f0f<\/strong><\/p>\n<\/p>\n<p><p><code>var<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u53d8\u91cf\uff0c\u8fd9\u79cd\u58f0\u660e\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u521d\u59cb\u503c\u53ef\u4ee5\u663e\u5f0f\u6307\u5b9a\uff0c\u4e5f\u53ef\u4ee5\u7701\u7565\u521d\u59cb\u503c\u800c\u4f7f\u7528\u7c7b\u578b\u7684\u96f6\u503c\u3002<code>var<\/code>\u58f0\u660e\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5c06\u53d8\u91cf\u58f0\u660e\u63d0\u5347\u5230\u51fd\u6570\u4f5c\u7528\u57df\u4e4b\u5916\u7684\u60c5\u51b5\u3002\u5b83\u7684\u901a\u7528\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">var \u53d8\u91cf\u540d \u7c7b\u578b = \u8868\u8fbe\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u82e5\u7701\u7565\u8868\u8fbe\u5f0f\uff0c\u5219\u53d8\u91cf\u521d\u59cb\u5316\u4e3a\u96f6\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5168\u5c40\u4f5c\u7528\u57df\u58f0\u660e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u4e4b\u5916\u4f7f\u7528<code>var<\/code>\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u58f0\u660e\u5305\u7ea7\u522b\u7684\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u4e9b\u53d8\u91cf\u5bf9\u6574\u4e2a\u5305\u5185\u7684\u6240\u6709\u51fd\u6570\u53ef\u89c1\u3002\u5982\u9700\u8de8\u5305\u4f7f\u7528\uff0c\u53d8\u91cf\u540d\u9700\u8981\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u7c7b\u578b\u63a8\u5bfc\u4e0e\u521d\u59cb\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u5728<code>var<\/code>\u58f0\u660e\u4e2d\uff0c\u82e5\u63d0\u4f9b\u4e86\u521d\u59cb\u503c\uff0c\u5219\u53ef\u4ee5\u7701\u7565\u53d8\u91cf\u7684\u7c7b\u578b\uff1b\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u63a8\u5bfc\u51fa\u53d8\u91cf\u7684\u51c6\u786e\u7c7b\u578b\u3002\u5982 <code>var x = &quot;Hello World&quot;<\/code>\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06<code>x<\/code>\u63a8\u5bfc\u4e3a<code>string<\/code>\u7c7b\u578b\u3002\u5f53\u6ca1\u6709\u521d\u59cb\u503c\u65f6\uff0c\u58f0\u660e\u5fc5\u987b\u5305\u62ec\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u77ed\u53d8\u91cf\u58f0\u660e\u65b9\u5f0f :=<\/strong><\/p>\n<\/p>\n<p><p><code>:=<\/code>\u662f\u77ed\u53d8\u91cf\u58f0\u660e\u64cd\u4f5c\u7b26\uff0c\u5b83\u7ed3\u5408\u4e86\u58f0\u660e\u548c\u521d\u59cb\u5316\u7684\u64cd\u4f5c\u3002\u4e0d\u540c\u4e8e<code>var<\/code>\u5173\u952e\u5b57\uff0c\u77ed\u53d8\u91cf\u58f0\u660e\u6ca1\u6709\u7c7b\u578b\u6807\u8bc6\uff0c\u56e0\u4e3a\u5b83\u6839\u636e\u53f3\u4fa7\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u81ea\u52a8\u63a8\u5bfc\u53d8\u91cf\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u51fd\u6570\u5185\u90e8\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u77ed\u53d8\u91cf\u58f0\u660e\u4ec5\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff0c\u800c\u4e14\u5b83\u5fc5\u987b\u521d\u59cb\u5316\u53d8\u91cf\u3002\u5982\u679c\u5c1d\u8bd5\u5728\u51fd\u6570\u5916\u90e8\u4f7f\u7528<code>:=<\/code>\uff0c\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u7c7b\u578b\u63a8\u5bfc\u4e0e\u8d4b\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u77ed\u53d8\u91cf\u58f0\u660e\u65f6\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u6307\u5b9a\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u53f3\u4fa7\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u81ea\u52a8\u63a8\u5bfc\u53d8\u91cf\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>x := 10<\/code>\uff0c\u7f16\u8bd1\u5668\u77e5\u9053<code>10<\/code>\u662f<code>int<\/code>\u7c7b\u578b\uff0c\u56e0\u6b64<code>x<\/code>\u4f1a\u88ab\u58f0\u660e\u4e3a<code>int<\/code>\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001VAR \u548c := \u7684\u9009\u62e9\u4f9d\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9<code>var<\/code>\u8fd8\u662f<code>:=<\/code>\u901a\u5e38\u53d6\u51b3\u4e8e\u4e0a\u4e0b\u6587\u4ee5\u53ca\u4e2a\u4eba\u7f16\u7801\u98ce\u683c\u3002\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u548c\u9884\u671f\u4f7f\u7528\u65b9\u5f0f\u6765\u9009\u62e9\u5408\u9002\u7684\u58f0\u660e\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u957f\u751f\u547d\u5468\u671f\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8f83\u957f\u751f\u547d\u5468\u671f\u7684\u53d8\u91cf\uff0c\u6216\u8005\u5728\u51fd\u6570\u4e4b\u5916\u9700\u8981\u8bbf\u95ee\u7684\u53d8\u91cf\uff0c\u5e94\u8be5\u4f7f\u7528<code>var<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u7c7b\u578b\u660e\u786e\u4e0e\u542b\u4e49\u6e05\u6670<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u660e\u786e\u6307\u51fa\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u6216\u8005\u5e0c\u671b\u4ee3\u7801\u66f4\u6e05\u695a\u5730\u8868\u8fbe\u5176\u610f\u56fe\u65f6\uff0c<code>var<\/code>\u58f0\u660e\u63d0\u4f9b\u4e86\u4e00\u4e2a\u597d\u5904\uff0c\u53ef\u4ee5\u5728\u58f0\u660e\u65f6\u663e\u5f0f\u6307\u5b9a\u53d8\u91cf\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u7efc\u5408\u5bf9\u6bd4\u4e0e\u5b9e\u9645\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684Go\u8bed\u8a00\u7f16\u7a0b\u4e2d\uff0c<code>var<\/code>\u548c<code>:=<\/code>\u4e24\u8005\u7684\u4f7f\u7528\u9891\u7387\u76f8\u5f53\uff0c\u4e86\u89e3\u5b83\u4eec\u4e4b\u95f4\u7684\u5dee\u5f02\u548c\u5404\u81ea\u7684\u4f7f\u7528\u573a\u5408\u53ef\u4ee5\u5e2e\u52a9\u7f16\u5199\u66f4\u6709\u6548\u7387\u548c\u66f4\u89c4\u8303\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5b9e\u4f8b\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u4f8b\u4e2d\u53ef\u4ee5\u660e\u663e\u770b\u5230<code>var<\/code>\u548c<code>:=<\/code>\u7528\u6cd5\u7684\u5dee\u5f02\u3002\u6bd4\u5982\u5728\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\u65f6\uff0c<code>var a, b, c = 1, 2, 3<\/code>\u4f1a\u6bd4<code>a, b, c := 1, 2, 3<\/code>\u66f4\u80fd\u663e\u793a\u7c7b\u578b\uff0c\u800c\u77ed\u53d8\u91cf\u58f0\u660e\u5219\u7528\u4e8e\u7b80\u6377\u5feb\u901f\u7684\u521d\u59cb\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u907f\u514d\u53d8\u91cf\u906e\u853d<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>:=<\/code>\u65f6\u9700\u8981\u6ce8\u610f\u5176\u53ef\u80fd\u5f15\u8d77\u7684\u53d8\u91cf\u906e\u853d\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u540c\u4e00\u4f5c\u7528\u57df\u4e2d\u591a\u6b21\u4f7f\u7528<code>:=<\/code>\u65f6\uff0c\u5bb9\u6613\u65e0\u610f\u4e2d\u5f15\u5165\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u66f4\u65b0\u5df2\u6709\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u7ed3\u8bba<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u800c\u8a00\u4e4b\uff0c<code>var<\/code>\u548c<code>:=<\/code>\u5728Go\u8bed\u8a00\u4e2d\u90fd\u662f\u53d8\u91cf\u58f0\u660e\u7684\u6709\u6548\u65b9\u6cd5\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4eec\u5404\u81ea\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5408\u7406\u8fd0\u7528\u8fd9\u4e24\u79cd\u58f0\u660e\u65b9\u5f0f\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u7684Go\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6709\u4ec0\u4e48\u4e0d\u540c\u7684\u58f0\u660e\u53d8\u91cf\u7684\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>var<\/code>\u5173\u952e\u5b57\u548c<code>:=<\/code>\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c\u53d8\u91cf\u58f0\u660e\u3002<\/p>\n<p><strong>2. var\u548c:=\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528<code>var<\/code>\u5173\u952e\u5b57\u58f0\u660e\u53d8\u91cf\u65f6\uff0c\u9700\u8981\u660e\u786e\u6307\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4f8b\u5982<code>var num int<\/code>\u3002<\/p>\n<p>\u800c\u4f7f\u7528<code>:=<\/code>\u64cd\u4f5c\u7b26\u8fdb\u884c\u53d8\u91cf\u58f0\u660e\u65f6\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u65ad\uff0c\u4f8b\u5982<code>num := 10<\/code>\u3002<\/p>\n<p><strong>3. \u4f55\u65f6\u5e94\u8be5\u4f7f\u7528var\uff0c\u4f55\u65f6\u4f7f\u7528:=\uff1f<\/strong><\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u660e\u786e\u77e5\u9053\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u5efa\u8bae\u4f7f\u7528<code>var<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\uff0c\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u800c\u5f53\u4f60\u4e0d\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u6216\u8005\u5e0c\u671b\u8ba9\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>:=<\/code>\u64cd\u4f5c\u7b26\u8fdb\u884c\u53d8\u91cf\u58f0\u660e\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5197\u4f59\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Go\u8bed\u8a00\u4e2d\uff0c\u53d8\u91cf\u58f0\u660e\u7684\u4e24\u79cd\u65b9\u5f0fvar\u548c:=\u786e\u5b9e\u5b58\u5728\u533a\u522b\uff1avar\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u3001\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u53d8\u91cf\u7684\u7c7b [&hellip;]","protected":false},"author":3,"featured_media":184463,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184448"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=184448"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184448\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/184463"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=184448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=184448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=184448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}