{"id":207016,"date":"2025-07-08T08:42:54","date_gmt":"2025-07-08T00:42:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207016\/"},"modified":"2025-07-08T08:42:54","modified_gmt":"2025-07-08T00:42:54","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97-go-%e4%b8%ad%e7%bb%93%e6%9e%84%e4%bd%93%e5%86%85%e9%83%a8%e5%ad%97%e6%ae%b5%e7%9a%84%e7%9c%9f%e5%ae%9e%e6%8c%87%e9%92%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207016\/","title":{"rendered":"\u5982\u4f55\u83b7\u5f97 Go \u4e2d\u7ed3\u6784\u4f53\u5185\u90e8\u5b57\u6bb5\u7684\u771f\u5b9e\u6307\u9488\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\u83b7\u5f97 Go \u4e2d\u7ed3\u6784\u4f53\u5185\u90e8\u5b57\u6bb5\u7684\u771f\u5b9e\u6307\u9488\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 15:12:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGolang\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u83b7\u5f97 Go \u4e2d\u7ed3\u6784\u4f53\u5185\u90e8\u5b57\u6bb5\u7684\u771f\u5b9e\u6307\u9488\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u7ed3\u6784\uff0c\u8bf4\uff1a<\/p>\n<pre>type sample struct{\n   data []float64\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u58f0\u660e\u4e00\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre>func (s sample) get() *[]float64{\nreturn &amp;s.data\n}<\/pre>\n<p>\u6211\u8bd5\u56fe\u901a\u8fc7 get() \u83b7\u5f97\u7684\u6307\u9488\u9644\u52a0\u5230\u6b64\u5207\u7247<\/p>\n<pre>func main(){\nexample := sample{[]float64{1,2,3}}\n\n\/\/here i will append:\npointertoarray := example.get()\n*pointertoarray = append(*pointertoarray, 4)\nfmt.println(example.get()) \/\/ still shows only &amp;{1,2,3}\n}<\/pre>\n<p>\u6211\u5bf9\u4e3a\u4ec0\u4e48\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u6709\u4e00\u4e2a\u5927\u6982\u7684\u4e86\u89e3\uff1a\u51fd\u6570 get \u8fd4\u56de\u5176\u672c\u5730\u8303\u56f4\u7684\u5730\u5740\uff0c\u6211\u5df2\u901a\u8fc7\u5c06\u7ed3\u6784\u672c\u8eab\u66f4\u6539\u4e3a\u6765\u4fee\u590d\u5b83<\/p>\n<pre>type sample struct{\ndata *[]float64\n}<\/pre>\n<p>\u4ee3\u7801\u8fd4\u56de\u9884\u671f\u7684 <code>&amp;{1,2,3,4}<\/code><\/p>\n<p>\u73b0\u5728\uff0c\u5bf9\u4e8e\u6211\u7684\u95ee\u9898\uff1a \u65e0\u8bba\u5982\u4f55\uff0c\u662f\u5426\u53ef\u4ee5\u901a\u8fc7 getter \u65b9\u6cd5\u83b7\u53d6\u6307\u5411\u7ed3\u6784\u4e2d\u5b57\u6bb5\u7684<strong>\u771f\u5b9e<\/strong>\u6307\u9488\uff0c\u800c\u4e0d\u76f4\u63a5\u5728\u7ed3\u6784\u4e2d\u4f7f\u7528\u6307\u9488\u5b57\u6bb5\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u662f\u60a8\u4f7f\u7528\u7ed3\u6784\u63a5\u6536\u5668\u800c\u4e0d\u662f\u6307\u9488\u5b9a\u4e49\u4e86 <code>get<\/code> \u65b9\u6cd5\u3002\u8fd9\u610f\u5473\u7740\u5f53\u60a8\u8fd4\u56de <code>&amp;s.data<\/code> \u65f6\uff0c\u60a8\u5c06\u83b7\u5f97\u63a5\u6536\u8005\u5b57\u6bb5\u7684\u5730\u5740\uff0c\u800c\u4e0d\u662f\u6765\u81ea\u539f\u59cb\u7ed3\u6784\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6307\u9488\u63a5\u6536\u5668\u7b80\u5355\u5730\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<pre>func (s *sample) get() *[]float64{\n    return &amp;s.data\n}<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u53ef\u8fd0\u884c\u793a\u4f8b ()<\/p>\n<pre>package main\n\nimport \"fmt\"\n\ntype sample struct {\n    data []float64\n}\n\nfunc (s *sample) Get() *[]float64 {\n    return &amp;s.data\n}\n\nfunc main() {\n    example := sample{[]float64{1, 2, 3}}\n\n    p := example.Get()\n    *p = append(*p, 4)\n    fmt.Println(example.Get())\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u83b7\u5f97 Go \u4e2d\u7ed3\u6784\u4f53\u5185\u90e8\u5b57\u6bb5\u7684\u771f\u5b9e\u6307\u9488\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-207016","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207016","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=207016"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207016\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}