{"id":41994,"date":"2024-12-01T09:31:32","date_gmt":"2024-12-01T01:31:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41994\/"},"modified":"2024-12-01T09:31:32","modified_gmt":"2024-12-01T01:31:32","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e%e4%bb%bb%e4%bd%95%e7%bb%93%e6%9e%84%e7%b1%bb%e5%9e%8b%e6%b4%be%e7%94%9f%e7%bb%93%e6%9e%84%e5%88%97%e8%a1%a8-%e4%bb%8e%e6%8e%a5%e5%8f%a3%e5%88%b0%e5%8f%af%e5%8f%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41994\/","title":{"rendered":"\u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}"},"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\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}<\/span><\/p>\n<h1>\u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 10:27:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u91c7\u7528\uff08\u4efb\u4f55\uff09\u7ed3\u6784\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u8fd9\u4e9b\u7ed3\u6784\u7684\u6570\u7ec4\u3002 returnarrayofstory \u7528\u56fa\u5b9a\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5c55\u793a\u4e86\u8fd9\u4e2a\u60f3\u6cd5\u3002<\/p>\n<p>\u5c1d\u8bd5\u4f7f\u7528\u51fd\u6570 returnarrayofx \u5bf9\u4efb\u4f55\u7c7b\u578b\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u4f46\u53cd\u5c04\u5728\u7f16\u8bd1\u65f6\u5931\u8d25\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\ntype story_t struct {\n    LANGUAGE string\n    SPECIES  string\n}\n\nfunc ReturnArrayOfStory(x story_t) []story_t {\n    x1 := x\n    var a1 []story_t\n    a1 = append(a1, x1)\n    a1 = append(a1, x1)\n    a1 = append(a1, x1)\n    return a1\n}\n\nfunc ReturnArrayOfX(x interface{}) []interface{} {\n    x1 := x\n    v1 := reflect.ValueOf(&amp;x1).Elem()\n    a1 := []reflect.TypeOf(&amp;x1)\n    \/\/  var a1 []x\n    a1 = append(a1, x1)\n    a1 = append(a1, x1)\n    a1 = append(a1, x1)\n    \/\/return a1\n    return a1\n}\n\nfunc main() {\n\n    var as1 []story_t\n\n    s1 := story_t{\"EN\", \"Prince of Persia\"}\n\n    as1 = ReturnArrayOfStory(s1)\n    \/\/as1 := ReturnArrayOfX(s1)\n    for i := 0; i &lt; len(as1); i++ {\n        fmt.Printf(\"%02d %+v\\n\", i, as1[i])\n    }\n\n    as2 := ReturnArrayOfX(s1)\n    \/\/as1 := ReturnArrayOfX(s1)\n    for i := 0; i &lt; len(as2); i++ {\n        fmt.Printf(\"%02d %+v\\n\", i, as2[i])\n    }\n\n}\n\na1 := []reflect.TypeOf(&amp;x1)\nmain.go:25:8: reflect.TypeOf is not a type<\/pre>\n<p><em>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u573a\u666f\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u559c\u6b22\u4ece\u6570\u636e\u5e93\u7b49\u5916\u90e8\u6570\u636e\u6e90\u8bfb\u53d6\u591a\u79cd\u7ed3\u6784\u7c7b\u578b\u3002<\/em><\/p>\n<ul>\n<li>\u5982\u4f55\u901a\u8fc7 returnarrayofx \u5b9e\u73b0\u6211\u7684\u76ee\u6807\uff1f <\/li>\n<li>\u5217\u8868\u9879 \u8fd9\u53ef\u80fd\u5417\uff1f\u5982\u679c\u4e0d\u662f\uff0c<strong>\u4e3a\u4ec0\u4e48<\/strong>\uff1f<\/li>\n<\/ul>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u95ee\u9898\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u9996\u5148\uff1a\u5982\u679c\u60a8\u60f3\u4f7f\u7528\u53cd\u5c04\u8fd4\u56de\u7c7b\u578b\u7684\u5207\u7247\uff1a<\/p>\n<pre>\/\/ you cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}\nfunc returnarrayofx(x interface{}) interface{} {\n    x1 := x\n    a1 := \n\/\/ this creates *[](typeof x)\nreflect.new(reflect.sliceof(reflect.typeof(x)))\n\/\/ append the first element to *[](typeof x)\n\/\/ after this, a1 now points to a slice, not to a slice *\n    a1 = reflect.append(a1.elem(), reflect.valueof(x1))\n    a1 = reflect.append(a1, reflect.valueof(x1))\n    a1 = reflect.append(a1, reflect.valueof(x1))\n    \/\/return [](typeof x)\n    return a1.interface()\n}<\/pre>\n<p>\u60a8\u53ef\u4ee5\u5c06\u5176\u7528\u4f5c\uff1a<\/p>\n<pre>as2 := returnarrayofx(s1)\narr:=as2.([]story_t)\nfor i := 0; i &lt; len(arr); i++ {\n    fmt.printf(\"%02d %+v\\n\", i, arr[i])\n}<\/pre>\n<p>\u7b2c\u4e8c\uff1a\u60a8\u53ef\u4ee5\u8fd4\u56de []interface{} \u800c\u65e0\u9700\u53cd\u5c04\uff1a<\/p>\n<pre>func returnarrayofx(x interface{}) []interface{} {\n   ret:=make([]interface{},0)\n   ret=append(ret,x)\n   ret=append(ret,x)\n   ret=append(ret,x)\n}<\/pre>\n<p>\u7136\u540e\u9700\u8981\u5904\u7406\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>as2 := returnarrayofx(s1)\nfor i := 0; i &lt; len(as2); i++ {\n    fmt.printf(\"%02d %+v\\n\", i, as2[i])\n    data:=as2[i].(story_t)\n}<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u901a\u7528\u5207\u7247\u8f6c\u6362\u51fd\u6570\uff1a<\/p>\n<pre>\/\/ convertslice copies the slice in src to the slice pointed to by pdst. \n\/\/ the concrete values in src must be assignable to the dst elements.\nfunc convertslice(pdst interface{}, src interface{}) {\n    dstv := reflect.valueof(pdst).elem()\n    srcv := reflect.valueof(src)\n    dstv.set(reflect.makeslice(dstv.type(), srcv.len(), srcv.len()))\n    for i := 0; i &lt; srcv.len(); i++ {\n        dstv.index(i).set(reflect.valueof(srcv.index(i).interface()))\n    }\n}<\/pre>\n<p>\u50cf\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>\/\/ Convert []story_t to []interface{}\n\ns0 := []story_t{{\"EN\", \"Prince of Persia\"}, {\"EN\", \"Karateka\"}}\nvar s1 []interface{}\nconvertSlice(&amp;s1, s0)\n\n\/\/ Convert []interface{} containing story_t to []story_t\n\nvar s2 []story_t\nconvertSlice(&amp;s2, s1)<\/pre>\n<p>\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{} \u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{} \u6765\u6e90\uff1astackoverflow 2024-04-22 10:27:38 0\u6d4f\u89c8 \u6536\u85cf \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300a\u5982\u4f55\u4ece\u4efb\u4f55\u7ed3\u6784\u7c7b\u578b\u6d3e\u751f\u7ed3\u6784\u5217\u8868 &#8211; \u4ece\u63a5\u53e3{}\u5230\u53ef\u53d8\u957f\u5ea6\u5207\u7247\u63a5\u53e3{}\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u91c7\u7528\uff08\u4efb\u4f55\uff09\u7ed3\u6784\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u8fd9\u4e9b\u7ed3\u6784\u7684\u6570\u7ec4\u3002 returnarrayofstory \u7528\u56fa\u5b9a\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u5c55\u793a\u4e86\u8fd9\u4e2a\u60f3\u6cd5\u3002 \u5c1d\u8bd5\u4f7f\u7528\u51fd\u6570 returnarrayofx \u5bf9\u4efb\u4f55\u7c7b\u578b\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u4f46\u53cd\u5c04\u5728\u7f16\u8bd1\u65f6\u5931\u8d25\u3002 package main import ( &#8220;fmt&#8221; &#8220;reflect&#8221; ) type story_t struct { LANGUAGE string SPECIES string } func ReturnArrayOfStory(x story_t) []story_t { x1 := x var a1 []story_t a1 = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41994","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41994"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41994\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}