{"id":207208,"date":"2025-07-08T11:25:03","date_gmt":"2025-07-08T03:25:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207208\/"},"modified":"2025-07-08T11:25:03","modified_gmt":"2025-07-08T03:25:03","slug":"go-%e8%af%ad%e8%a8%80%e7%9a%84-setter","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207208\/","title":{"rendered":"Go \u8bed\u8a00\u7684 Setter"},"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>Go \u8bed\u8a00\u7684 Setter<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 20:15:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aGo \u8bed\u8a00\u7684 Setter\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f88\u62b1\u6b49\u95ee\u4e86\u8fd9\u4e2a\u57fa\u672c\u95ee\u9898\u3002\u6211\u662f go \u8bed\u8a00\u65b0\u624b\u3002<\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u540d\u4e3a <code>protectedcustomtype<\/code> \u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u6211\u4e0d\u5e0c\u671b\u8c03\u7528\u8005\u76f4\u63a5\u5c06\u5176\u4e2d\u7684\u53d8\u91cf\u8bbe\u7f6e\u4e3a <code>set<\/code>\uff0c\u800c\u662f\u5e0c\u671b\u4f7f\u7528 <code>getter<\/code> \/ <code>setter<\/code> \u65b9\u6cd5\u6765\u6267\u884c\u6b64\u64cd\u4f5c<\/p>\n<p>\u4e0b\u9762\u662f\u6211\u7684 <code>protectedcustomtype<\/code><\/p>\n<pre>package custom\n\ntype protectedcustomtype struct {\n    name string\n    age int\n    phonenumber int\n}\n\nfunc setage (pct *protectedcustomtype, age int)  {\n    pct.age=age\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u7684 <code>main<\/code> \u51fd\u6570<\/p>\n<pre>import (\n    \"fmt\"\n    \".\/custom\"\n)\nvar print =fmt.Println\n\nfunc structCheck2() {\n    pct := ProtectedCustomType{}\n    custom.SetAge(pct,23)\n\n    print (pct.Name)\n}\n\nfunc main() {\n    \/\/structCheck()\n    structCheck2()\n}<\/pre>\n<p>\u4f46\u662f\u6211\u65e0\u6cd5\u7ee7\u7eed\u4e0b\u53bb..\u4f60\u80fd\u5e2e\u6211\u4e86\u89e3\u5982\u4f55\u5728 golang \u4e2d\u5b9e\u73b0 getter-setter \u6982\u5ff5\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6709setter\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u65b9\u6cd5\u58f0\u660e\uff1a<\/p>\n<pre>func(pct *protectedcustomtype) setage (age int)  {\n    pct.age = age\n}<\/pre>\n<p>\u7136\u540e\u60a8\u5c06\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre>pct.SetAge(23)<\/pre>\n<p>\u8fd9\u79cd\u58f0\u660e\u4f7f\u60a8\u80fd\u591f\u5728\u7ed3\u6784\u4e0a\u6267\u884c\u51fd\u6570\uff0c \u901a\u8fc7\u4f7f\u7528<\/p>\n<p><code>(pct *protectedcustomtype)<\/code> <\/p>\n<p>\u60a8\u6b63\u5728\u5c06\u6307\u9488\u4f20\u9012\u7ed9\u60a8\u7684\u7ed3\u6784\uff0c\u56e0\u6b64\u5bf9\u5176\u8fdb\u884c\u7684\u64cd\u4f5c\u4f1a\u66f4\u6539\u5176\u5185\u90e8 \u4ee3\u8868\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 \u4e0b\u9605\u8bfb\u6709\u5173\u6b64\u7c7b\u529f\u80fd\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u6216\u8005 \u4e0b \u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207208","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207208","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=207208"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207208\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}