{"id":207708,"date":"2025-07-08T13:14:19","date_gmt":"2025-07-08T05:14:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207708\/"},"modified":"2025-07-08T13:14:19","modified_gmt":"2025-07-08T05:14:19","slug":"%e6%98%af%e5%90%a6%e5%8f%af%e4%bb%a5%e5%9c%a8%e4%b8%8d%e4%bd%bf%e7%94%a8%e6%8e%a5%e5%8f%a3%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e4%bd%bf%e5%85%b6%e5%b7%a5%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207708\/","title":{"rendered":"\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u4f7f\u5176\u5de5\u4f5c\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>\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u4f7f\u5176\u5de5\u4f5c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 20:45:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u4f7f\u5176\u5de5\u4f5c\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4e3a\u4ec0\u4e48\u5f53\u6211\u6dfb\u52a0\u7c7b\u578b <code>pet []interface{name()}<\/code> \u65f6\u4ee5\u4e0b\u5de5\u4f5c\u6709\u6548\uff0c\u4f46\u5f53\u6211\u6dfb\u52a0\u7c7b\u578b <code>pet []string<\/code> \u65f6\u4ee5\u4e0b\u5de5\u4f5c\u4e0d\u5de5\u4f5c\uff1f\u662f\u5426\u53ef\u4ee5<strong>\u4e0d<\/strong>\u4f7f\u7528\u754c\u9762\u4f7f\u5176\u5de5\u4f5c\uff1f<\/p>\n<pre>package main\n\nimport \"fmt\"\n\ntype Pet []string \/\/ cannot use Cat(\"Puss\") (type Cat) as type string in array or slice literal\n\/\/ type Pet []interface{Name()} \/\/ prt Fluffy\n\ntype Cat string\n\nfunc (c Cat) Name() {\n    fmt.Println(c)\n}\n\nfunc main() {\n    p := Pet{Cat(\"Whiskers\"), Cat(\"Fluffy\")} \n    p1 := p[1]\n    p1.Name() \n}\n\n.\/oo3.go:15:14: cannot use Cat(\"Whiskers\") (type Cat) as type string in array or slice literal\n.\/oo3.go:15:31: cannot use Cat(\"Fluffy\") (type Cat) as type string in array or slice literal\n.\/oo3.go:17:4: p1.Name undefined (type string has no field or method Name)<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u58f0\u660e\u4e3a <code>[]string<\/code> \u65f6\uff0c\u7c7b\u578b <code>pet<\/code> \u65e0\u6cd5\u4f7f\u7528 <code>cat<\/code> \u7c7b\u578b\u7684\u503c\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u56e0\u4e3a <code>cat<\/code> \u548c <code>string<\/code> \u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u8fd9\u5c31\u662f go \u7c7b\u578b\u7cfb\u7edf\u7684\u5de5\u4f5c\u539f\u7406\u3002\u5f53\u60a8\u5c06\u65b0\u7c7b\u578b\u5b9a\u4e49\u4e3a <code>type name othertype<\/code> \u65f6\uff0c<code>name<\/code> \u6210\u4e3a\u4e00\u4e2a\u5168\u65b0\u7c7b\u578b\uff0c\u5176\u5185\u5b58\u5e03\u5c40\u4e0e\u57fa\u7840\u7c7b\u578b\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u65b0\u7c7b\u578b\u4e0d\u4f1a\u5177\u6709\u4e3a <code>othertype<\/code> \u5b9a\u4e49\u7684\u4efb\u4f55\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u5c06 <code>cat<\/code> \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>p := Pet{string(Cat(\"Whiskers\")), string(Cat(\"Fluffy\"))}<\/pre>\n<p>\u90a3\u4e48<code>pet<\/code>\u4ecd\u7136\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002<\/p>\n<p>\u5f53\u60a8\u4f7f\u7528 <code>name<\/code> \u65b9\u6cd5\u5c06 <code>pet<\/code> \u5b9a\u4e49\u4e3a\u63a5\u53e3\u6570\u7ec4\u65f6\uff0c\u5219 <code>cat<\/code> \u73b0\u5728\u53ef\u7528\u4e8e\u521d\u59cb\u5316 <code>pet<\/code> \u7684\u5143\u7d20\uff0c\u56e0\u4e3a <code>cat<\/code> \u5b9e\u73b0\u4e86 <code>name<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff1a<code>pet<\/code> \u4f5c\u4e3a <code>[]string<\/code> \u4ec5\u4fdd\u5b58\u5b57\u7b26\u4e32\u503c\u3002 <code>pet<\/code> \u4f5c\u4e3a <code>[]interface{name{}}<\/code> \u4fdd\u5b58\u5b9e\u73b0 <code>name<\/code> \u65b9\u6cd5\u7684\u4efb\u4f55\u503c\u3002\u5982\u679c\u60a8\u9700\u8981\u5728 <code>pet<\/code> \u5143\u7d20\u4e0a\u8c03\u7528 <code>name<\/code> \u65b9\u6cd5\uff0c\u90a3\u4e48\u60a8\u5fc5\u987b\u4f7f\u7528\u63a5\u53e3\u6765\u5b8c\u6210\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u4f7f\u5176\u5de5\u4f5c\uff1f\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\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; &#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-207708","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207708","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=207708"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207708\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}