{"id":208284,"date":"2025-07-08T17:35:48","date_gmt":"2025-07-08T09:35:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208284\/"},"modified":"2025-07-08T17:35:48","modified_gmt":"2025-07-08T09:35:48","slug":"golang%e5%a6%82%e4%bd%95%e7%be%8e%e5%8c%96%e6%90%9c%e7%b4%a2%e5%94%af%e4%b8%80id%e9%80%bb%e8%be%91","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208284\/","title":{"rendered":"Golang\u5982\u4f55\u7f8e\u5316\u641c\u7d22\u552f\u4e00id\u903b\u8f91"},"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>Golang\u5982\u4f55\u7f8e\u5316\u641c\u7d22\u552f\u4e00id\u903b\u8f91<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 11:51:30<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\u5982\u4f55\u7f8e\u5316\u641c\u7d22\u552f\u4e00id\u903b\u8f91\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7f16\u5199\u4e86\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u68c0\u6d4b\u7ed3\u679c\u662f\u5426\u6709\u8d85\u8fc7 1 \u4e2a\u5177\u6709\u503c\u7684 somestruct\uff0c\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5219\u8fd4\u56de anotherstruct.id\u3002\u901a\u5e38\u7ed3\u679c\u53ea\u6709\u4e00\u4e2a somestruct \u6709\u4ef7\u503c\uff0c\u5176\u4f59\u90fd\u662f\u7a7a\u7684\uff0c\u7136\u540e\u6211\u5c06\u5f97\u5230 anotherstruct \u7684 id\u3002\u4f60\u53ef\u4ee5\u9605\u8bfb\u6211\u4e0b\u9762\u7684\u903b\u8f91\uff0c\u903b\u8f91\u662f\u6b63\u786e\u7684\uff0c\u4f46\u5bf9\u6211\u6765\u8bf4\u770b\u8d77\u6765\u5f88\u96be\u770b\uff0c\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u5417\uff1f<\/p>\n<pre>var tmp []string\nfor _, id := range result {\n    if len(id.SomeStruct) &gt; 0 {\n        tmp = append(tmp, id.AnotherStruct.ID)\n    }\n}\n\nif len(tmp) &gt; 1 {\n    return \"Failure, \", fmt.Errorf(\"More than 1 id that has unique code\")\n} else {\n    return tmp[0], nil\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u6240\u8981\u505a\u7684\u5c31\u662f\u5b58\u50a8\u6765\u81ea\u5176\u4ed6\u7ed3\u6784\u7684 id\uff0c\u5e76\u786e\u4fdd\u60a8\u7684 id \u4e0d\u8d85\u8fc7 1\u3002<\/p>\n<p>\u8fd9\u662f\u5bf9 @s4eed3sm \u7b54\u6848\u7684\u6269\u5c55\uff1a<\/p>\n<pre>var tmp string\nfor _, o := range result {\n    if len(o.somestruct) &gt; 0 {\n        if len(tmp) &gt; 0 {\n            return \"failure, \", fmt.errorf(\"more than 1 id that has unique code\")\n        }\n        tmp = o.anotherstruct.id\n    }\n}\nreturn tmp, nil<\/pre>\n<p>\u6211\u4e0d\u5b8c\u5168\u7406\u89e3\u4f60\u7684\u903b\u8f91\u548c\u7528\u4f8b\uff0c\u4f46\u6700\u540e\u4e00\u4e2a else \u662f\u591a\u4f59\u7684\u800c\u4e14\u4e0d\u60ef\u7528\u3002<\/p>\n<pre>var tmp []string\nfor _, id := range result {\n    if len(id.SomeStruct) &gt; 0 {\n        tmp = append(tmp, id.AnotherStruct.ID)\n    }\n}\n\nif len(tmp) &gt; 1 {\n    return \"Failure, \", fmt.Errorf(\"More than 1 id that has unique code\")\n}\n\/\/ else was redundant\n\nreturn tmp[0], nil<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aGolang\u5982\u4f55\u7f8e\u5316\u641c\u7d22\u552f\u4e00id\u903b\u8f91\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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-208284","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208284","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=208284"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208284\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}