{"id":207947,"date":"2025-07-08T09:28:06","date_gmt":"2025-07-08T01:28:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207947\/"},"modified":"2025-07-08T09:28:06","modified_gmt":"2025-07-08T01:28:06","slug":"%e6%88%91%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%bd%9c%e4%b8%ba-interface-%e5%8f%82%e6%95%b0%e4%bc%a0%e9%80%92","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207947\/","title":{"rendered":"\u6211\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a &#8230;interface{} \u53c2\u6570\u4f20\u9012"},"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>\u6211\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a &#8230;interface{} \u53c2\u6570\u4f20\u9012<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 09:45: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\u6211\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a &#8230;interface{} \u53c2\u6570\u4f20\u9012\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>\u5f53\u53c2\u6570\u4e3a <code>...strings<\/code> \u65f6\uff0c\u6211\u77e5\u9053\u5982\u4f55\u4f20\u9012\u5b83<\/p>\n<p>\u4f46\u662f\u8fd9\u4e2a\u4f8b\u5b50\u6709\u70b9\u4e0d\u540c\uff1a<\/p>\n<pre>func main() {\n    args := []string{\"hello\", \"world\"}\n    fmt.Println(args...)\n}\n<\/pre>\n<p>https:\/\/play.golang.org\/p\/7eldqqvdpvr<\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u629b\u51fa\u9519\u8bef <code>cannot use args (type [] string) as type [] interface {} in argument to fmt.println<\/code><\/p>\n<p>\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u6700\u60ef\u7528\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u4e4b\u4e00\u5b8c\u6210\uff1a<\/p>\n<pre>args := []interface{}{\"hello\", \"world\"}\n   fmt.println(args...)<\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>args:=[]string{\"hello\", \"world\"}\n  iargs:=make([]interface{},0)\n  for _,x:=range args {\n     iargs=append(iargs, x)\n  }\n  fmt.Println(iargs...)<\/pre>\n<p>\u60a8\u53ef\u4ee5\u5728\u9700\u8981 <code>interface{}<\/code> \u7684\u60c5\u51b5\u4e0b\u4f20\u9012 <code>string<\/code>\uff0c\u4f46\u5728\u9700\u8981 <code>[]interface{}<\/code> \u7684\u60c5\u51b5\u4e0b\u4e0d\u80fd\u4f20\u9012 <code>[]string<\/code>\u3002\u7f16\u8bd1\u5668\u4f1a\u5c06 <code>string<\/code> \u503c\u8f6c\u6362\u4e3a <code>interface{}<\/code> \u503c\uff0c\u4f46\u6570\u7ec4\u4e0d\u4f1a\u8fd9\u6837\u505a\uff0c\u60a8\u5fc5\u987b\u81ea\u5df1\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u6211\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a &#8230;interface{} \u53c2\u6570\u4f20\u9012\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-207947","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207947","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=207947"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207947\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}