{"id":3185,"date":"2025-03-25T23:50:23","date_gmt":"2025-03-25T15:50:23","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3185"},"modified":"2025-03-25T23:50:23","modified_gmt":"2025-03-25T15:50:23","slug":"go-%e8%af%ad%e8%a8%80%e6%8c%87%e9%92%88%ef%bc%88pointer%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e6%8c%87%e9%92%88%ef%bc%88pointer%ef%bc%89\/","title":{"rendered":"Go \u8bed\u8a00\u6307\u9488\uff08Pointer\uff09"},"content":{"rendered":"\n<p>Go \u8bed\u8a00\u4e2d\u7684\u6307\u9488\u662f\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u7684\u5f15\u7528\uff0c\u6307\u9488\u5b58\u50a8\u7684\u662f\u5176\u4ed6\u53d8\u91cf\u7684\u5730\u5740\uff0c\u53ef\u4ee5\u7528\u6765\u95f4\u63a5\u64cd\u4f5c\u53d8\u91cf\u7684\u503c\u3002\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\uff0cGo \u7684\u6307\u9488\u76f8\u6bd4 C \u8bed\u8a00\u66f4\u52a0\u5b89\u5168\uff0c\u56e0\u4e3a Go \u4e0d\u652f\u6301\u6307\u9488\u8fd0\u7b97\uff08\u5982\u6307\u9488\u52a0\u51cf\u7b49\uff09\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. \u6307\u9488\u57fa\u7840<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u6307\u9488\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u4fdd\u5b58\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u901a\u8fc7\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u8be5\u5730\u5740\u4e0a\u7684\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u5b9a\u4e49\u6307\u9488<\/strong><\/h3>\n\n\n\n<p>Go \u4e2d\u901a\u8fc7 <code>*<\/code> \u6765\u58f0\u660e\u6307\u9488\u7c7b\u578b\uff0c\u6307\u9488\u7684\u7c7b\u578b\u662f\u5b83\u6240\u6307\u5411\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ptr *int  \/\/ \u58f0\u660e\u4e00\u4e2a\u6307\u5411 int \u7c7b\u578b\u7684\u6307\u9488<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>*int<\/code> \u8868\u793a\u6307\u5411 <code>int<\/code> \u7c7b\u578b\u7684\u6307\u9488\u3002<\/li>\n\n\n\n<li><code>ptr<\/code> \u662f\u6307\u9488\u53d8\u91cf\uff0c\u5b83\u5b58\u50a8\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u6307\u5411\u4e00\u4e2a <code>int<\/code> \u7c7b\u578b\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u83b7\u53d6\u53d8\u91cf\u7684\u5730\u5740<\/strong><\/h3>\n\n\n\n<p>\u8981\u83b7\u53d6\u4e00\u4e2a\u53d8\u91cf\u7684\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>&amp;<\/code> \u64cd\u4f5c\u7b26\uff0c<code>&amp;<\/code> \u8fd4\u56de\u7684\u662f\u8be5\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x := 58\nptr := &amp;x  \/\/ ptr \u5b58\u50a8 x \u7684\u5185\u5b58\u5730\u5740\nfmt.Println(ptr)   \/\/ \u8f93\u51fa\u6307\u9488\u5730\u5740\nfmt.Println(*ptr)  \/\/ \u8f93\u51fa\u6307\u9488\u6307\u5411\u7684\u503c\uff0c\u5373 58<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&amp;x<\/code> \u8fd4\u56de <code>x<\/code> \u7684\u5730\u5740\uff0c\u5b58\u50a8\u5728 <code>ptr<\/code> \u4e2d\u3002<\/li>\n\n\n\n<li><code>*ptr<\/code> \u7528\u4e8e\u8bbf\u95ee <code>ptr<\/code> \u6307\u5411\u7684\u503c\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>2. \u89e3\u5f15\u7528\u6307\u9488\uff08Dereferencing\uff09<\/strong><\/h2>\n\n\n\n<p>\u89e3\u5f15\u7528\u662f\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u5176\u6240\u6307\u5411\u7684\u503c\u3002\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 <code>*<\/code> \u88ab\u7528\u4e8e\u83b7\u53d6\u6307\u9488\u6240\u6307\u5411\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x := 58\nptr := &amp;x  \/\/ ptr \u6307\u5411 x \u7684\u5185\u5b58\u5730\u5740\nfmt.Println(*ptr)  \/\/ \u8f93\u51fa 58\uff0c\u89e3\u5f15\u7528\u6307\u9488\uff0c\u8bbf\u95ee\u6307\u9488\u6307\u5411\u7684\u503c<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>*ptr<\/code> \u8868\u793a\u89e3\u5f15\u7528 <code>ptr<\/code>\uff0c\u5373\u83b7\u53d6 <code>ptr<\/code> \u6307\u5411\u7684\u503c\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>3. \u6307\u9488\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u6307\u9488\u5e38\u5e38\u7528\u4e8e\u51fd\u6570\u53c2\u6570\u4f20\u9012\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u60f3\u8ba9\u51fd\u6570\u4fee\u6539\u4f20\u5165\u7684\u53d8\u91cf\u65f6\u3002\u4f20\u9012\u6307\u9488\u6bd4\u4f20\u9012\u5927\u91cf\u6570\u636e\u7684\u526f\u672c\u66f4\u9ad8\u6548\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u5bf9\u8c61\u6216\u7ed3\u6784\u4f53\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u4f20\u9012\u6307\u9488\u7ed9\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc updateValue(ptr *int) {\n    *ptr = 100  \/\/ \u4fee\u6539\u6307\u9488\u6307\u5411\u7684\u503c\n}\n\nfunc main() {\n    x := 58\n    fmt.Println(\"Before:\", x)  \/\/ \u8f93\u51fa\uff1aBefore: 58\n    updateValue(&amp;x)  \/\/ \u4f20\u9012 x \u7684\u5730\u5740\n    fmt.Println(\"After:\", x)  \/\/ \u8f93\u51fa\uff1aAfter: 100\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>updateValue<\/code> \u51fd\u6570\u901a\u8fc7\u6307\u9488\u4fee\u6539\u4e86 <code>x<\/code> \u7684\u503c\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>4. \u6307\u9488\u4e0e\u6570\u7ec4\u3001\u5207\u7247<\/strong><\/h2>\n\n\n\n<p>\u6307\u9488\u5e38\u5e38\u4e0e\u6570\u7ec4\u548c\u5207\u7247\u4e00\u8d77\u4f7f\u7528\u3002\u6307\u9488\u53ef\u4ee5\u76f4\u63a5\u6307\u5411\u6570\u7ec4\u6216\u5207\u7247\u4e2d\u7684\u5143\u7d20\uff0c\u64cd\u4f5c\u5b83\u4eec\u7684\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u6307\u5411\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p>\u6307\u9488\u53ef\u4ee5\u6307\u5411\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u901a\u8fc7\u6307\u9488\u95f4\u63a5\u4fee\u6539\u6570\u7ec4\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr := &#91;3]int{1, 2, 3}\nptr := &amp;arr&#91;0]  \/\/ \u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n*ptr = 100\nfmt.Println(arr)  \/\/ \u8f93\u51fa\uff1a&#91;100 2 3]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u6307\u5411\u5207\u7247<\/strong><\/h3>\n\n\n\n<p>\u5207\u7247\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u5e95\u5c42\u6570\u7ec4\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u7684\u6307\u9488\u6765\u4fee\u6539\u5e95\u5c42\u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr := &#91;]int{1, 2, 3}\nptr := &amp;arr&#91;0]  \/\/ \u6307\u5411\u5207\u7247\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n*ptr = 100\nfmt.Println(arr)  \/\/ \u8f93\u51fa\uff1a&#91;100 2 3]<\/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. <code>new<\/code> \u548c <code>make<\/code> \u4e0e\u6307\u9488<\/strong><\/h2>\n\n\n\n<p>Go \u63d0\u4f9b\u4e86 <code>new<\/code> \u548c <code>make<\/code> \u4e24\u4e2a\u5185\u5efa\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5b83\u4eec\u4e0e\u6307\u9488\u76f8\u5173\uff0c\u4f46\u7528\u9014\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 <code>new<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p><code>new<\/code> \u7528\u4e8e\u5206\u914d\u5185\u5b58\u5e76\u521d\u59cb\u5316\u4e3a\u96f6\u503c\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5185\u5b58\u7684\u6307\u9488\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr := new(int)  \/\/ \u521b\u5efa\u4e00\u4e2a\u6307\u5411 int \u7c7b\u578b\u7684\u6307\u9488\nfmt.Println(*ptr)  \/\/ \u8f93\u51fa\uff1a0\uff0c\u56e0\u4e3a new \u521d\u59cb\u5316\u4e3a\u96f6\u503c\n*ptr = 58\nfmt.Println(*ptr)  \/\/ \u8f93\u51fa\uff1a58<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>new(int)<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u6307\u5411 <code>int<\/code> \u7c7b\u578b\u7684\u6307\u9488\uff0c\u5185\u5b58\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2 <code>make<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p><code>make<\/code> \u7528\u4e8e\u521b\u5efa\u5207\u7247\u3001\u6620\u5c04\uff08map\uff09\u548c\u901a\u9053\uff08channel\uff09\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5207\u7247\u3001\u6620\u5c04\u6216\u901a\u9053\uff0c\u800c\u4e0d\u662f\u6307\u9488\u3002\u4e0e <code>new<\/code> \u4e0d\u540c\uff0c<code>make<\/code> \u4e0d\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u65b0\u5bf9\u8c61\u7684\u6307\u9488\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := make(&#91;]int, 5)  \/\/ \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a 5 \u7684\u5207\u7247\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;0 0 0 0 0]<\/code><\/pre>\n\n\n\n<p><code>make<\/code> \u8fd4\u56de\u4e00\u4e2a\u521d\u59cb\u5316\u8fc7\u7684\u5207\u7247\uff0c\u6620\u5c04\u6216\u901a\u9053\uff0c\u800c <code>new<\/code> \u8fd4\u56de\u7684\u662f\u6307\u5411\u96f6\u503c\u7684\u6307\u9488\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>6. \u6307\u9488\u7684\u96f6\u503c<\/strong><\/h2>\n\n\n\n<p>\u6307\u9488\u7684\u96f6\u503c\u662f <code>nil<\/code>\uff0c\u8868\u793a\u8be5\u6307\u9488\u6ca1\u6709\u6307\u5411\u4efb\u4f55\u6709\u6548\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ptr *int  \/\/ ptr \u9ed8\u8ba4\u4e3a nil\nfmt.Println(ptr)  \/\/ \u8f93\u51fa\uff1a&lt;nil&gt;<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u5c1d\u8bd5\u89e3\u5f15\u7528\u4e00\u4e2a <code>nil<\/code> \u6307\u9488\uff0cGo \u4f1a\u629b\u51fa\u8fd0\u884c\u65f6\u9519\u8bef\uff08<code>panic<\/code>\uff09\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>7. \u6307\u9488\u7684\u7528\u9014\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f20\u9012\u5927\u5bf9\u8c61<\/strong>\uff1a\u901a\u8fc7\u6307\u9488\u4f20\u9012\u5bf9\u8c61\u53ef\u4ee5\u907f\u514d\u590d\u5236\u5927\u91cf\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u4fee\u6539\u53c2\u6570<\/strong>\uff1a\u901a\u8fc7\u4f20\u9012\u6307\u9488\uff0c\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u8c03\u7528\u8005\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u6307\u9488\u7528\u4e8e\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c<code>new<\/code> \u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6307\u9488\u3002<\/li>\n\n\n\n<li><strong>\u5207\u7247\u4e0e\u6570\u7ec4<\/strong>\uff1a\u6307\u9488\u5e38\u7528\u4e8e\u64cd\u4f5c\u5207\u7247\u548c\u6570\u7ec4\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>8. \u6307\u9488\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6307\u9488\u662f\u5b58\u50a8\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u7528\u6765\u95f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u503c\u3002<\/li>\n\n\n\n<li>Go \u4e2d\u7684\u6307\u9488\u53ef\u4ee5\u901a\u8fc7 <code>*<\/code> \u83b7\u53d6\u6307\u9488\u6240\u6307\u5411\u7684\u503c\uff0c\u901a\u8fc7 <code>&amp;<\/code> \u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u6307\u9488\u662f\u51fd\u6570\u53c2\u6570\u4f20\u9012\u65f6\u4fee\u6539\u6570\u636e\u7684\u5e38\u89c1\u65b9\u5f0f\uff0c\u5c24\u5176\u7528\u4e8e\u4f20\u9012\u5927\u6570\u636e\u7ed3\u6784\u3002<\/li>\n\n\n\n<li>Go \u4f7f\u7528 <code>new<\/code> \u548c <code>make<\/code> \u6765\u5206\u914d\u5185\u5b58\u548c\u521b\u5efa\u5bf9\u8c61\uff0c\u4f46\u5b83\u4eec\u6709\u4e0d\u540c\u7684\u7528\u9014\u548c\u884c\u4e3a\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html#pointers\">Go \u5b98\u65b9\u6587\u6863 &#8211; Pointers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/Pointers\">Go Wiki &#8211; Pointers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u4e2d\u7684\u6307\u9488\u662f\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u7684\u5f15\u7528\uff0c\u6307\u9488\u5b58\u50a8\u7684\u662f\u5176\u4ed6\u53d8\u91cf\u7684\u5730\u5740\uff0c\u53ef\u4ee5\u7528\u6765\u95f4\u63a5\u64cd\u4f5c\u53d8\u91cf\u7684\u503c\u3002\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3186,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3185","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\/3185","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=3185"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3185\/revisions"}],"predecessor-version":[{"id":3187,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3185\/revisions\/3187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3186"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}