{"id":207476,"date":"2025-07-08T10:16:40","date_gmt":"2025-07-08T02:16:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207476\/"},"modified":"2025-07-08T10:16:40","modified_gmt":"2025-07-08T02:16:40","slug":"%e9%99%84%e5%8a%a0%e5%88%b0%e5%88%87%e7%89%87%e6%97%b6%e6%8e%92%e5%ba%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207476\/","title":{"rendered":"\u9644\u52a0\u5230\u5207\u7247\u65f6\u6392\u5e8f\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>\u9644\u52a0\u5230\u5207\u7247\u65f6\u6392\u5e8f\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 21:51:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u9644\u52a0\u5230\u5207\u7247\u65f6\u6392\u5e8f\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a <code>[]byte<\/code>\uff0c\u6211\u9700\u8981\u6309\u5347\u5e8f\u5bf9\u5b83\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u6211\u83b7\u53d6\u4e00\u4e2a\u5305\u542b\u9879\u76ee\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u8fed\u4ee3\u8be5\u6570\u7ec4\u4ee5\u521b\u5efa\u8fd4\u56de\u7684\u5bf9\u8c61\uff1a<\/p>\n<pre>\/\/ unfortunately, for some obscure reason I can't change the data types of the caller and the object from the function call are different, although both are []byte underneath (...)\n\ntype ID []byte\n\/\/ in another package:\ntype ByteInterface []byte\n\n\nfunc (c *Store) GetAll() ByteInterface {\n  returnObj := make([]ByteInterface,0)\n  obj, err := GetData()\n  \/\/ err handling\n  for _, b := range obj.IDs {\n     returnObj = append(returnObj, ByteInterface(b))\n  }\n  return returnObj\n}<\/pre>\n<p>\u6240\u4ee5\u6211\u95ee\u81ea\u5df1\u662f\u5426\u53ef\u4ee5\u6267\u884c <code>append<\/code> \u4ee5\u4fbf\u7acb\u5373\u5bf9 <code>returnobj<\/code> \u8fdb\u884c\u6392\u5e8f\uff0c\u6216\u8005\u662f\u5426\u9700\u8981\u9884\u5148\u5bf9 <code>obj.bytedata<\/code> \u8fdb\u884c\u6392\u5e8f\uff08\u6216\u4e4b\u540e\u5bf9 <code>returnojb<\/code> \u8fdb\u884c\u6392\u5e8f\uff09\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\n<p>\u589e\u52a0\u76ee\u6807\u5207\u7247\uff08\u53ef\u80fd\u91cd\u65b0\u5206\u914d\u5b83\uff09\uff1a<\/p>\n<pre>numelems := len(returnobj)\nreturnobj = append(returnobj, make([]byte, len(obj))...)<\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u6807\u51c6\u63d2\u5165\u65b9\u6cd5\uff0c\u901a\u8fc7\u627e\u5230\u4e00\u4e2a\u4f4d\u7f6e\u6765\u9010\u4e00\u653e\u7f6e\u6e90\u5207\u7247\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\uff0c\u4ee5\u4fdd\u6301\u76ee\u6807\u6392\u5e8f\uff1a<\/p>\n<pre>for _, b := range obj {\n  i := sort.Search(numElems, func (i int) bool {\n    return returnObj[i] &gt;= b\n  }\n  if i &lt; numElems {\n    copy(returnObj[i+1:], returnObj[i:])\n  }\n  returnObj[i] = b\n  numElems++\n}<\/pre>\n<p>\uff08\u5bf9 <code>copy<\/code> \u7684\u8c03\u7528\u5e94\u8be5\u901a\u8fc7\u51cf\u5c11\u590d\u5236\u6765\u4f18\u5316\uff0c\u4f46\u8fd9\u7559\u7ed9\u8bfb\u8005\u4f5c\u4e3a\u7ec3\u4e60\u3002\uff09<\/p>\n<\/li>\n<\/ol>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u9644\u52a0\u5230\u5207\u7247\u65f6\u6392\u5e8f\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207476","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207476","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=207476"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207476\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}