{"id":207796,"date":"2025-07-08T10:17:08","date_gmt":"2025-07-08T02:17:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207796\/"},"modified":"2025-07-08T10:17:08","modified_gmt":"2025-07-08T02:17:08","slug":"%e6%9b%b4%e6%96%b0%e4%bc%a0%e9%80%92%e7%bb%99%e5%87%bd%e6%95%b0%e7%9a%84%e7%bb%93%e6%9e%84%e4%b8%ad%e7%9a%84%e5%88%87%e7%89%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207796\/","title":{"rendered":"\u66f4\u65b0\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u7ed3\u6784\u4e2d\u7684\u5207\u7247"},"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>\u66f4\u65b0\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u7ed3\u6784\u4e2d\u7684\u5207\u7247<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 17:48: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>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u66f4\u65b0\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u7ed3\u6784\u4e2d\u7684\u5207\u7247\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u9700\u8981\u5e2e\u52a9\u4e86\u89e3\u5982\u4f55\u66f4\u65b0\u7ed3\u6784\u4e2d\u5305\u542b\u5e76\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u5207\u7247\u3002 <\/p>\n<p>\u51fd\u6570addbooktoshelfinlibrary(l *library, shelfid int, b book) &#8211; \u5c06\u56fe\u4e66\u9986\u4f5c\u4e3a\u8f93\u5165\uff0c\u5c1d\u8bd5\u5c06\u4e66\u7c4d\u6dfb\u52a0\u5230\u4e66\u67b6\uff0cid = shelfid\uff08\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff09\u3002\u8be5\u51fd\u6570\u9644\u52a0\u5230 books \u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9 books \u6570\u7ec4\u3002\u6211\u7f3a\u5c11\u4ec0\u4e48\uff1f <\/p>\n<p>\u5728\u4ee3\u7801\u8fd0\u884c\u7ed3\u675f\u65f6\uff0c\u6211\u5e0c\u671b\u8fd9\u4e9b\u4e66\u4e2d\u5305\u542b\u4e24\u672c\u4e66\uff0c\u201c\u54c8\u5229\u6ce2\u7279\u201d\uff0c\u201c\u5723\u7ecf\u201d\uff0c\u4f46\u6211\u53ea\u770b\u5230\u4e00\u672c\uff0c\u5373\u54c8\u5229\u6ce2\u7279\u3002\u53e6\u5916\uff0c\u6211\u6b63\u5728\u4f20\u9012\u4e00\u4e2a\u6307\u5411\u5e93\u7684\u6307\u9488\uff0c\u4f46\u6211\u8ba4\u4e3a\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8fd9\u5e76\u4e0d\u91cd\u8981\u3002 <\/p>\n<p>\u6f14\u793a\u4ee3\u7801\uff1a- https:\/\/play.golang.org\/p\/jrjtlsj-jhi<\/p>\n<pre>func main() {\n    lib := library{\n        shelves: []shelf{\n            {\n                id: 1,\n                books: []book{\n                    {name: \"harrypotter\"},\n                },\n            },\n        },\n    }\n    addBookToShelfInLibrary(&amp;lib, 1, book{name: \"bible\"})\n    fmt.Printf(\"%v\", lib)\n\n}\n\ntype library struct {\n    shelves []shelf\n}\ntype shelf struct {\n    id    int\n    books []book\n}\n\ntype book struct {\n    name string\n}\n\nfunc addBookToShelfInLibrary(l *library, shelfid int, b book) {\n    for _, s := range (*l).shelves {\n        if s.id == shelfid {\n            \/\/found shelf, add book\n            s.books = append(s.books, b)\n        }\n    }\n}<\/pre>\n<p>\u611f\u8c22\u60a8\u63d0\u524d\u7684\u7b54\u590d\/\u89e3\u91ca\u3002 <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u6b64\u58f0\u660e\u4e2d\uff1a<\/p>\n<pre>s := range (*l).shelves<\/pre>\n<p>\u53d8\u91cf <code>s<\/code> \u662f\u5207\u7247\u5143\u7d20\u7684\u526f\u672c\u3002\u7a0d\u540e\u5bf9\u8ffd\u52a0\u7684\u8c03\u7528\u4f1a\u4fee\u6539\u6b64\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5207\u7247\u5143\u7d20\u3002\u4fee\u6539\u4ee3\u7801\u4fee\u6539\u5207\u7247\u5143\u7d20\uff1a<\/p>\n<pre>func addbooktoshelfinlibrary(l *library, shelfid int, b book) {\n    for i := range l.shelves {\n        s := &amp;l.shelves[i]\n        if s.id == shelfid {\n            \/\/found shelf, add book\n            s.books = append(s.books, b)\n        }\n    }\n}<\/pre>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6307\u5411\u67b6\u5b50\u7684\u6307\u9488\uff1a<\/p>\n<pre>type library struct {\n    shelves []*shelf\n}\n\nlib := library{\n    shelves: []*shelf{\n        {\n ...<\/pre>\n<p>\u6240\u6709\u5176\u4ed6\u4ee3\u7801\u4fdd\u6301\u4e0d\u53d8\u3002 \u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207796","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207796","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=207796"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207796\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}