{"id":3179,"date":"2025-03-25T23:47:58","date_gmt":"2025-03-25T15:47:58","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3179"},"modified":"2025-03-25T23:47:58","modified_gmt":"2025-03-25T15:47:58","slug":"go-%e8%af%ad%e8%a8%80%e5%8f%98%e9%87%8f%e4%bd%9c%e7%94%a8%e5%9f%9f%ef%bc%88variable-scope%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e5%8f%98%e9%87%8f%e4%bd%9c%e7%94%a8%e5%9f%9f%ef%bc%88variable-scope%ef%bc%89\/","title":{"rendered":"Go \u8bed\u8a00\u53d8\u91cf\u4f5c\u7528\u57df\uff08Variable Scope\uff09"},"content":{"rendered":"\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<strong>\u53d8\u91cf\u4f5c\u7528\u57df<\/strong>\u5b9a\u4e49\u4e86\u53d8\u91cf\u53ef\u88ab\u8bbf\u95ee\u7684\u533a\u57df\uff0c\u901a\u5e38\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong><\/li>\n\n\n\n<li><strong>\u5757\u4f5c\u7528\u57df<\/strong><\/li>\n\n\n\n<li><strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/li>\n\n\n\n<li><strong>\u5305\u4f5c\u7528\u57df<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u4e00\u4e2a\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u548c\u53ef\u8bbf\u95ee\u8303\u56f4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f5c\u7528\u57df\u7684\u5206\u7c7b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u51fd\u6570\u4f5c\u7528\u57df\uff08Function Scope\uff09<\/strong><\/h3>\n\n\n\n<p>\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5c40\u9650\u4e8e\u5176\u58f0\u660e\u6240\u5728\u7684\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u4ec5\u5728\u8be5\u51fd\u6570\u5185\u90e8\u53ef\u89c1\u548c\u6709\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc foo() {\n    x := 10  \/\/ \u53d8\u91cf x \u53ea\u5728\u51fd\u6570 foo \u5185\u90e8\u53ef\u89c1\n    fmt.Println(\"\u51fd\u6570\u5185\u7684 x:\", x)\n}\n\nfunc main() {\n    foo()\n    \/\/ fmt.Println(x)  \/\/ \u9519\u8bef\uff0cx \u4e0d\u5728 main \u51fd\u6570\u4e2d\u53ef\u89c1\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u51fd\u6570\u5185\u7684 x: 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u5757\u4f5c\u7528\u57df\uff08Block Scope\uff09<\/strong><\/h3>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u4ee3\u7801\u5757\uff08\u4f8b\u5982\uff1a<code>if<\/code> \u8bed\u53e5\u3001<code>for<\/code> \u5faa\u73af\u3001<code>switch<\/code> \u8bed\u53e5\u7b49\uff09\u5185\u58f0\u660e\u7684\u53d8\u91cf\u53ea\u80fd\u5728\u8be5\u5757\u5185\u8bbf\u95ee\u3002\u4e00\u65e6\u79bb\u5f00\u8be5\u4ee3\u7801\u5757\uff0c\u53d8\u91cf\u5c31\u4e0d\u518d\u6709\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    if true {\n        x := 100  \/\/ \u53d8\u91cf x \u53ea\u5728 if \u8bed\u53e5\u5757\u5185\u6709\u6548\n        fmt.Println(\"if \u5185\u7684 x:\", x)\n    }\n    \/\/ fmt.Println(x)  \/\/ \u9519\u8bef\uff0cx \u4e0d\u5728 if \u8bed\u53e5\u5757\u5916\u53ef\u89c1\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if \u5185\u7684 x: 100<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 \u5168\u5c40\u4f5c\u7528\u57df\uff08Global Scope\uff09<\/strong><\/h3>\n\n\n\n<p>\u5168\u5c40\u4f5c\u7528\u57df\u662f\u6307\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u53d8\u91cf\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u53d8\u91cf\u662f\u5728\u5305\u7ea7\u522b\uff08\u5373\u6587\u4ef6\u9876\u90e8\uff09\u5b9a\u4e49\u7684\u3002\u5b83\u4eec\u53ef\u4ee5\u88ab\u5305\u5185\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nvar x = 10  \/\/ \u5168\u5c40\u53d8\u91cf\uff0c\u4f5c\u7528\u4e8e\u6574\u4e2a\u5305\n\nfunc foo() {\n    fmt.Println(\"\u5728 foo \u4e2d\u8bbf\u95ee x:\", x)  \/\/ \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\n}\n\nfunc main() {\n    fmt.Println(\"\u5728 main \u4e2d\u8bbf\u95ee x:\", x)  \/\/ \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\n    foo()  \/\/ \u8c03\u7528 foo \u51fd\u6570\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5728 main \u4e2d\u8bbf\u95ee x: 10\n\u5728 foo \u4e2d\u8bbf\u95ee x: 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.4 \u5305\u4f5c\u7528\u57df\uff08Package Scope\uff09<\/strong><\/h3>\n\n\n\n<p>\u5305\u4f5c\u7528\u57df\u6307\u7684\u662f\u5728 Go \u5305\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u901a\u5e38\u4f7f\u7528\u9996\u5b57\u6bcd\u5927\u5199\u6765\u8868\u793a\u8be5\u53d8\u91cf\u6216\u51fd\u6570\u53ef\u4ee5\u88ab\u5176\u4ed6\u5305\u8bbf\u95ee\u3002\u5982\u679c\u9996\u5b57\u6bcd\u5c0f\u5199\uff0c\u5219\u8868\u793a\u8be5\u53d8\u91cf\u6216\u51fd\u6570\u4ec5\u5728\u672c\u5305\u5185\u6709\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nvar publicVar = 20   \/\/ \u5305\u4f5c\u7528\u57df\uff0c\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u5176\u4ed6\u5305\u53ef\u8bbf\u95ee\nvar privateVar = 30  \/\/ \u5305\u4f5c\u7528\u57df\uff0c\u9996\u5b57\u6bcd\u5c0f\u5199\uff0c\u4ec5\u5728\u672c\u5305\u5185\u53ef\u8bbf\u95ee\n\nfunc main() {\n    fmt.Println(\"publicVar:\", publicVar)  \/\/ \u53ef\u4ee5\u8bbf\u95ee publicVar\n    fmt.Println(\"privateVar:\", privateVar)  \/\/ \u53ea\u80fd\u8bbf\u95ee privateVar \u5728\u672c\u5305\u5185\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>publicVar: 20\nprivateVar: 30<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u53d8\u91cf\u7684\u751f\u547d\u5468\u671f<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c40\u90e8\u53d8\u91cf\uff08\u51fd\u6570\u4f5c\u7528\u57df\u548c\u5757\u4f5c\u7528\u57df\uff09<\/strong>\uff1a\u5728\u51fd\u6570\u6216\u4ee3\u7801\u5757\u5185\u5b9a\u4e49\uff0c\u751f\u547d\u5468\u671f\u4ec5\u9650\u4e8e\u8be5\u51fd\u6570\u6216\u4ee3\u7801\u5757\u7684\u6267\u884c\u5468\u671f\u3002\u4e00\u65e6\u9000\u51fa\u4f5c\u7528\u57df\uff0c\u53d8\u91cf\u4f1a\u88ab\u9500\u6bc1\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u4ece\u7a0b\u5e8f\u5f00\u59cb\u5230\u7ed3\u675f\u90fd\u53ef\u7528\uff0c\u5176\u751f\u547d\u5468\u671f\u662f\u6574\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u5468\u671f\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u91cf<\/strong>\uff1a\u5e38\u91cf\u7684\u4f5c\u7528\u57df\u548c\u53d8\u91cf\u76f8\u4f3c\uff0c\u4f46\u5b83\u7684\u503c\u5728\u6574\u4e2a\u4f5c\u7528\u57df\u5185\u662f\u4e0d\u53ef\u6539\u53d8\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u53d8\u91cf\u906e\u853d\uff08Variable Shadowing\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u8f83\u5c0f\u7684\u4f5c\u7528\u57df\u5185\uff08\u5982\u51fd\u6570\u6216\u4ee3\u7801\u5757\u5185\uff09\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0e\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u540c\u540d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u5185\u5c42\u53d8\u91cf\u4f1a\u906e\u853d\uff08\u8986\u76d6\uff09\u5916\u5c42\u53d8\u91cf\u3002\u5373\uff0c\u5916\u90e8\u7684\u53d8\u91cf\u5728\u5185\u5c42\u4f5c\u7528\u57df\u5185\u4e0d\u53ef\u89c1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nvar x = 100  \/\/ \u5916\u90e8\u5168\u5c40\u53d8\u91cf\n\nfunc main() {\n    var x = 200  \/\/ \u5c40\u90e8\u53d8\u91cf\uff0c\u906e\u853d\u4e86\u5168\u5c40\u53d8\u91cf\n    fmt.Println(\"\u5c40\u90e8 x:\", x)  \/\/ \u8f93\u51fa\u5c40\u90e8\u53d8\u91cf x\n    \/\/ \u5916\u90e8 x \u88ab\u906e\u853d\uff0c\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c40\u90e8 x: 200<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. <code>defer<\/code> \u8bed\u53e5\u548c\u4f5c\u7528\u57df<\/strong><\/h2>\n\n\n\n<p><code>defer<\/code> \u8bed\u53e5\u7528\u4e8e\u6ce8\u518c\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5f53\u524d\u51fd\u6570\u9000\u51fa\u65f6\u6267\u884c\u3002<code>defer<\/code> \u6ce8\u518c\u7684\u51fd\u6570\u4f1a\u5728\u5f53\u524d\u51fd\u6570\u7ed3\u675f\u65f6\u6309\u7167\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u987a\u5e8f\u6267\u884c\u3002\u56e0\u6b64\uff0c<code>defer<\/code> \u8bed\u53e5\u4e5f\u53d7\u4f5c\u7528\u57df\u9650\u5236\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    defer fmt.Println(\"\u8fd9\u6761\u8bed\u53e5\u5728 main \u51fd\u6570\u9000\u51fa\u65f6\u6267\u884c\")\n\n    fmt.Println(\"\u8fd9\u662f main \u51fd\u6570\u5185\u7684\u8bed\u53e5\")\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fd9\u662f main \u51fd\u6570\u5185\u7684\u8bed\u53e5\n\u8fd9\u6761\u8bed\u53e5\u5728 main \u51fd\u6570\u9000\u51fa\u65f6\u6267\u884c<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong><\/td><td>\u53d8\u91cf\u4ec5\u5728\u51fd\u6570\u5185\u6709\u6548\uff0c\u4e00\u65e6\u9000\u51fa\u51fd\u6570\uff0c\u53d8\u91cf\u88ab\u9500\u6bc1\u3002<\/td><\/tr><tr><td><strong>\u5757\u4f5c\u7528\u57df<\/strong><\/td><td>\u53d8\u91cf\u5728\u7279\u5b9a\u4ee3\u7801\u5757\u5185\u6709\u6548\uff0c\u79bb\u5f00\u5757\u540e\u4e0d\u518d\u53ef\u8bbf\u95ee\u3002<\/td><\/tr><tr><td><strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/td><td>\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u6709\u6548\uff0c\u53ef\u4ee5\u88ab\u6240\u6709\u51fd\u6570\u8bbf\u95ee\u3002<\/td><\/tr><tr><td><strong>\u5305\u4f5c\u7528\u57df<\/strong><\/td><td>\u53d8\u91cf\u4ec5\u5728\u5305\u5185\u6709\u6548\uff0c\u9996\u5b57\u6bcd\u5c0f\u5199\u65f6\u4ec5\u5305\u5185\u53ef\u89c1\uff0c\u9996\u5b57\u6bcd\u5927\u5199\u65f6\u5305\u5916\u53ef\u89c1\u3002<\/td><\/tr><tr><td><strong>\u53d8\u91cf\u906e\u853d<\/strong><\/td><td>\u5185\u5c42\u4f5c\u7528\u57df\u7684\u540c\u540d\u53d8\u91cf\u4f1a\u906e\u853d\u5916\u5c42\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u3002<\/td><\/tr><tr><td><strong><code>defer<\/code><\/strong><\/td><td>\u5ef6\u8fdf\u6267\u884c\u7684\u51fd\u6570\u53d7\u4f5c\u7528\u57df\u63a7\u5236\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Go \u8bed\u8a00\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u7ba1\u7406\u975e\u5e38\u6e05\u6670\uff0c\u8fd9\u5bf9\u4e8e\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u7ef4\u62a4\u81f3\u5173\u91cd\u8981\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u53d8\u91cf\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u53d8\u91cf\u53ef\u88ab\u8bbf\u95ee\u7684\u533a\u57df\uff0c\u901a\u5e38\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\uff1a \u4e0d\u540c\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u4e00\u4e2a\u53d8\u91cf\u7684\u751f\u547d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3180,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3179"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3179\/revisions"}],"predecessor-version":[{"id":3181,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3179\/revisions\/3181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3180"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}