{"id":294459,"date":"2024-05-20T15:10:46","date_gmt":"2024-05-20T07:10:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/294459.html"},"modified":"2024-05-20T15:11:01","modified_gmt":"2024-05-20T07:11:01","slug":"go-%e7%bc%96%e7%a8%8b%e6%8c%87%e9%92%88%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/294459.html","title":{"rendered":"Go \u7f16\u7a0b\u6307\u9488\u51fd\u6570\u5982\u4f55\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27141436\/805ebd34-1509-402a-a21a-59fd756439b7.webp\" alt=\"Go \u7f16\u7a0b\u6307\u9488\u51fd\u6570\u5982\u4f55\u5b9e\u73b0\" \/><\/p>\n<p><p>\u5728Go\u8bed\u8a00\u4e2d\uff0c<strong>\u7f16\u7a0b\u6307\u9488\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4fee\u6539\u6570\u636e<\/strong>\u3001<strong>\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387<\/strong>\u3001<strong>\u5141\u8bb8\u51fd\u6570\u548c\u65b9\u6cd5\u80fd\u591f\u76f4\u63a5\u64cd\u4f5c\u5bf9\u8c61\u5b9e\u4f8b<\/strong>\u3002\u8fd9\u4e9b\u80fd\u529b\u4f7f\u5f97\u7f16\u7a0b\u6a21\u5f0f\u66f4\u52a0\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u76f4\u63a5\u4fee\u6539\u6216\u8005\u4f18\u5316\u5185\u5b58\u548c\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u5c24\u4e3a\u91cd\u8981\u3002\u5176\u4e2d\uff0c<strong>\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4fee\u6539\u6570\u636e<\/strong>\u662f\u6307\u9488\u51fd\u6570\u6700\u76f4\u63a5\u7684\u5e94\u7528\uff0c\u8fd9\u5141\u8bb8\u51fd\u6570\u63a5\u6536\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u5e76\u76f4\u63a5\u5bf9\u8be5\u5730\u5740\u4e2d\u7684\u503c\u8fdb\u884c\u4fee\u6539\uff0c\u4ece\u800c\u5f71\u54cd\u5230\u51fd\u6570\u5916\u90e8\u7684\u539f\u59cb\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u6307\u9488\u7684\u4e00\u4e2a\u6838\u5fc3\u539f\u56e0\u662f\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u503c\u4f20\u9012\uff08\u4f20\u9012\u6570\u636e\u7684\u526f\u672c\uff09\uff0c\u5f15\u7528\u4f20\u9012\uff08\u4f20\u9012\u6570\u636e\u7684\u5730\u5740\uff09\u5728\u5904\u7406\u5927\u578b\u7ed3\u6784\u4f53\u6216\u8005\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u7684\u4f7f\u7528\u53ca\u590d\u5236\u7684\u5f00\u9500\u3002\u56e0\u6b64\uff0c\u5728\u8bbe\u8ba1\u51fd\u6570\u548c\u65b9\u6cd5\u65f6\uff0c\u5408\u7406\u5e94\u7528\u6307\u9488\u4e0d\u4ec5\u80fd\u63d0\u5347\u6027\u80fd\uff0c\u8fd8\u80fd\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6709\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001GO \u8bed\u8a00\u4e2d\u7684\u6307\u9488\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u63a2\u8ba8\u6307\u9488\u51fd\u6570\u5982\u4f55\u5b9e\u73b0\u4e4b\u524d\uff0c\u4e86\u89e3Go\u8bed\u8a00\u4e2d\u7684\u6307\u9488\u57fa\u7840\u662f\u5341\u5206\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u6307\u9488\u7684\u58f0\u660e\u548c\u521d\u59cb\u5316<\/strong>\uff1aGo\u8bed\u8a00\u7684\u6307\u9488\u58f0\u660e\u4f7f\u7528 <code>*<\/code> \u7b26\u53f7\u4f5c\u4e3a\u524d\u7f00\uff0c\u6307\u5411\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u521d\u59cb\u5316\u6307\u9488\u7684\u5e38\u89c4\u65b9\u6cd5\u662f\u4f7f\u7528 <code>&amp;<\/code> \u7b26\u53f7\u53d6\u53d8\u91cf\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2aint\u7c7b\u578b\u7684\u53d8\u91cf <code>a<\/code> \uff0c\u7136\u540e\u58f0\u660e\u4e00\u4e2a\u6307\u5411 <code>a<\/code> \u7684\u6307\u9488 <code>p<\/code> :<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">var a int = 58<\/p>\n<p>var p *int = &amp;a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6307\u9488\u7684\u96f6\u503c<\/strong>\uff1a\u5728Go\u8bed\u8a00\u4e2d\uff0c\u6ca1\u6709\u521d\u59cb\u5316\u7684\u6307\u9488\u7684\u96f6\u503c\u662f <code>nil<\/code>\uff0c\u8fd9\u8868\u793a\u8be5\u6307\u9488\u65e2\u4e0d\u6307\u5411\u4efb\u4f55\u6709\u6548\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u548c\u4fee\u6539\u503c<\/strong>\uff1a\u901a\u8fc7\u6307\u9488\u53ef\u4ee5\u8bbf\u95ee\u76ee\u6807\u5bf9\u8c61\u7684\u503c\uff0c\u8fd9\u79f0\u4e3a\u201c\u95f4\u63a5\u5f15\u7528\u201d\u6216\u201c\u89e3\u5f15\u7528\u201d\uff0c\u5728Go\u4e2d\u4f7f\u7528 <code>*<\/code> \u7b26\u53f7\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u5b9e\u73b0\u6307\u9488\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u6307\u9488\u51fd\u6570\uff0c\u5173\u952e\u5728\u4e8e\u8ba9\u51fd\u6570\u7684\u53c2\u6570\u662f\u6307\u9488\u7c7b\u578b\uff0c\u4ece\u800c\u80fd\u591f\u76f4\u63a5\u64cd\u4f5c\u53c2\u6570\u6307\u9488\u6307\u5411\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f16\u5199\u6307\u9488\u51fd\u6570<\/strong>\uff1a\u4e3a\u4e86\u901a\u8fc7\u51fd\u6570\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u7684\u53c2\u6570\u5e94\u8be5\u662f\u5bf9\u5e94\u53d8\u91cf\u7684\u6307\u9488\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">func modifyValue(p *int) {<\/p>\n<p>    *p = 10<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>modifyValue<\/code> \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6307\u5411 <code>int<\/code> \u7c7b\u578b\u6570\u636e\u7684\u6307\u9488 <code>p<\/code> \u4f5c\u4e3a\u53c2\u6570\u3002\u5728\u51fd\u6570\u5185\u90e8\uff0c\u4f7f\u7528 <code>*p<\/code> \u4fee\u6539\u4e86\u6307\u9488\u6240\u6307\u5411\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u8c03\u7528\u6307\u9488\u51fd\u6570<\/strong>\uff1a\u8c03\u7528\u4e0a\u8ff0\u6307\u9488\u51fd\u6570\u65f6\uff0c\u9700\u8981\u5c06\u53d8\u91cf\u7684\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">var value int = 5<\/p>\n<p>modifyValue(&amp;value)<\/p>\n<p>fmt.Println(value) \/\/ \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6307\u9488\u51fd\u6570\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4fee\u6539\u4f20\u5165\u7684\u6570\u636e<\/strong>\uff1a\u6700\u76f4\u63a5\u7684\u4f7f\u7528\u573a\u666f\u662f\u9700\u8981\u5728\u51fd\u6570\u5185\u4fee\u6539\u4f20\u5165\u7684\u53c2\u6570\u503c\u3002\u901a\u8fc7\u6307\u9488\u51fd\u6570\uff0c\u53ef\u4ee5\u907f\u514d\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u5236\uff0c\u76f4\u63a5\u5728\u539f\u59cb\u6570\u636e\u4e0a\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u5316\u6027\u80fd<\/strong>\uff1a\u5f53\u51fd\u6570\u9700\u8981\u5904\u7406\u5927\u578b\u6570\u636e\u7ed3\u6784\uff08\u5982\u5927\u6570\u7ec4\u3001\u5927\u7ed3\u6784\u4f53\u7b49\uff09\u65f6\uff0c\u4f7f\u7528\u6307\u9488\u4f20\u9012\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u7684\u4f7f\u7528\u548c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p><strong>\u5171\u4eab\u6570\u636e<\/strong>\uff1a\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u591a\u4e2agoroutine\u53ef\u80fd\u9700\u8981\u8bbf\u95ee\u548c\u4fee\u6539\u540c\u4e00\u4efd\u6570\u636e\u3002\u4f7f\u7528\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540cgoroutine\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6307\u9488\u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p><strong>\u907f\u514d\u7a7a\u6307\u9488\u89e3\u5f15\u7528<\/strong>\uff1a\u5728\u4f7f\u7528\u6307\u9488\u51fd\u6570\u65f6\uff0c\u8981\u786e\u4fdd\u4f20\u5165\u7684\u6307\u9488\u4e0d\u662f <code>nil<\/code>\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u629b\u51fa\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e76\u53d1\u5b89\u5168\u6027<\/strong>\uff1a\u5f53\u591a\u4e2agoroutine\u540c\u65f6\u64cd\u4f5c\u540c\u4e00\u4e2a\u6307\u9488\u6307\u5411\u7684\u6570\u636e\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5e76\u53d1\u5b89\u5168\u6027\uff0c\u907f\u514d\u51fa\u73b0\u7ade\u6001\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong>\uff1a\u867d\u7136Go\u8bed\u8a00\u6709\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u4f46\u662f\u4e0d\u5f53\u7684\u6307\u9488\u4f7f\u7528\u4ecd\u7136\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u957f\u671f\u6301\u6709\u67d0\u4e9b\u5df2\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u53ef\u80fd\u963b\u6b62\u5783\u573e\u6536\u96c6\u5668\u56de\u6536\u8fd9\u4e9b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u6307\u9488\u51fd\u6570\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528Go\u8bed\u8a00\u7684\u7279\u6027\uff0c\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u7a0b\u5e8f\u3002\u6307\u9488\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u6539\u5584\u6027\u80fd\uff0c\u4e5f\u4f7f\u5f97\u51fd\u6570\u64cd\u4f5c\u7684\u8303\u56f4\u66f4\u52a0\u5e7f\u6cdb\uff0c\u4f46\u540c\u65f6\u4e5f\u9700\u8981\u6ce8\u610f\u907f\u514d\u9519\u8bef\u7684\u6307\u9488\u64cd\u4f5c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fGo\u7f16\u7a0b\u4e2d\u7684\u6307\u9488\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u6307\u9488\u51fd\u6570\u662fGo\u7f16\u7a0b\u4e2d\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\u4f5c\u4e3a\u5176\u8fd4\u56de\u503c\u3002\u6307\u9488\u51fd\u6570\u53ef\u7528\u4e8e\u52a8\u6001\u5206\u914d\u5185\u5b58\u3001\u8de8\u51fd\u6570\u4f20\u9012\u53c2\u6570,\u4ee5\u53ca\u5728\u51fd\u6570\u8c03\u7528\u4e4b\u540e\u4ecd\u80fd\u4fee\u6539\u53d8\u91cf\u7684\u503c\u7b49\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd4\u56de\u6307\u9488\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u7684\u4f5c\u7528\u57df\u4e4b\u5916\u4f7f\u7528\u51fd\u6570\u5185\u90e8\u521b\u5efa\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u58f0\u660e\u548c\u4f7f\u7528\u6307\u9488\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u8981\u58f0\u660e\u6307\u9488\u51fd\u6570\uff0c\u9700\u8981\u5728\u51fd\u6570\u540d\u4e4b\u524d\u52a0\u4e0a\u4e00\u4e2a\u661f\u53f7\uff08*\uff09\uff0c\u4ee5\u6807\u8bc6\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-go\">func createPointer() *int {\n    var num = 10\n    return &amp;num\n}\n\nfunc m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {\n    var p *int\n    p = createPointer()\n    fmt.Println(*p) \/\/ \u8f93\u51fa: 10\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>createPointer<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411 <code>num<\/code> \u53d8\u91cf\u7684\u6307\u9488\uff0c\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\u6211\u4eec\u5c06\u5176\u8d4b\u503c\u7ed9 <code>p<\/code>\uff0c\u7136\u540e\u901a\u8fc7 <code>*p<\/code> \u89e3\u5f15\u7528\u6307\u9488\u5e76\u8f93\u51fa\u5176\u503c\u3002<\/p>\n<p><strong>3. \u6307\u9488\u51fd\u6570\u548c\u666e\u901a\u51fd\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>\u6307\u9488\u51fd\u6570\u5728\u8fd4\u56de\u503c\u4e2d\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\uff0c\u800c\u666e\u901a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5177\u4f53\u7684\u503c\u3002\u6307\u9488\u51fd\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u5e76\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u4f7f\u5f97\u5728\u51fd\u6570\u4f5c\u7528\u57df\u4e4b\u5916\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528\u8be5\u53d8\u91cf\u3002\u53e6\u5916\uff0c\u901a\u8fc7\u6307\u9488\u51fd\u6570\u8fd4\u56de\u7684\u6307\u9488\u53ef\u4ee5\u5728\u5916\u90e8\u4fee\u6539\u53d8\u91cf\u7684\u503c\uff0c\u8fd9\u662f\u666e\u901a\u51fd\u6570\u6240\u4e0d\u80fd\u5b9e\u73b0\u7684\u3002\u6307\u9488\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u5904\u7406\u8f83\u5927\u7684\u6570\u636e\u7ed3\u6784\u6216\u9700\u8981\u8de8\u51fd\u6570\u4f20\u9012\u548c\u4fee\u6539\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Go\u8bed\u8a00\u4e2d\uff0c\u7f16\u7a0b\u6307\u9488\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u7684\u65b9\u5f0f\u4fee\u6539\u6570\u636e\u3001\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3001\u5141\u8bb8\u51fd\u6570\u548c\u65b9\u6cd5\u80fd\u591f\u76f4\u63a5\u64cd\u4f5c\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":294483,"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\/294459"}],"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=294459"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294459\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/294483"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=294459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=294459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=294459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}