{"id":207355,"date":"2025-07-08T08:47:17","date_gmt":"2025-07-08T00:47:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207355\/"},"modified":"2025-07-08T08:47:17","modified_gmt":"2025-07-08T00:47:17","slug":"%e5%85%b3%e4%ba%8e%e6%8e%a5%e5%8f%a3%e5%88%86%e9%85%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207355\/","title":{"rendered":"\u5173\u4e8e\u63a5\u53e3\u5206\u914d"},"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>\u5173\u4e8e\u63a5\u53e3\u5206\u914d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 21:57: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>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGolang\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5173\u4e8e\u63a5\u53e3\u5206\u914d\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f53\u5c06\u7ed3\u6784\u4f53\u6307\u9488\u5206\u914d\u7ed9\u63a5\u53e3\u65f6\uff0c\u4e3a\u4ec0\u4e48 go \u4e0d\u8ba4\u4e3a\u8fd9\u662f\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef\uff1f<\/p>\n<pre>package main\n\nimport \"fmt\"\n\ntype ABC interface {\n    a() string\n    b() int\n}\n\ntype XYZ struct {\n    aa string\n    bb int\n}\n\nfunc (xyz XYZ) a() string {\n    return \"XYZ\"\n}\n\nfunc (xyz XYZ) b() int {\n    return 123\n}\n\nfunc main() {\n    var xyz *XYZ\n    var abc ABC = xyz \/\/ type of abc is *main.XYZ\uff0cI think that Golang can find this error here, but why not?\n\n    fmt.Printf(\"%T\\n\", abc)\n\n    a, ret := abc.(*XYZ)\n    fmt.Println(a, ret) \/\/ type of a is *main.XYZ\n\n    fmt.Println(a.a()) \/\/ will occur a error, because the type of a(*main.XYZ) not implements the interface ABC\n}\n\n<\/pre>\n<p>\u6211\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48 go \u4e0d\u8ba4\u4e3a\u8fd9\u662f\u201cvar abc abc = xyz\u201d\u7684\u9519\u8bef<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>xyz \u786e\u5b9e\u5b9e\u73b0\u4e86 abc\u3002\u8fd9\u4e0e \u7684\u786e\u5b9a\u65b9\u5f0f\u6709\u5173\uff08\u6dfb\u52a0\u5f3a\u8c03\uff09\uff1a<\/p>\n<p>\uff1a<\/p>\n<p>\u5f53\u8c03\u7528<code>*xyz.a()<\/code>\u65f6\uff0cgo\u7f16\u8bd1\u5668\u603b\u662f\u53ef\u4ee5\u81ea\u52a8\u89e3\u5f15\u7528\u6307\u9488\u4ee5\u83b7\u5f97\u503c\u63a5\u6536\u8005\u3002\u8fd9\u6837\u505a\u6ca1\u6709\u4efb\u4f55\u7f3a\u70b9\uff0c\u56e0\u4e3a\u63a5\u6536\u8005\u65e0\u6cd5\u4fee\u6539\uff08\u5c31\u8c03\u7528\u8005\u800c\u8a00\uff09\u3002<\/p>\n<p>\u5f53\u4e14\u4ec5\u5f53\u8be5\u503c\u662f\u53ef\u5bfb\u5740\u65f6\uff0c\u9006\u8fd0\u7b97\u624d\u6210\u7acb\uff1a<\/p>\n<pre>type T struct {}\nfunc (*T) M()\n\nfunc main() {\n    var t T\n    t.M() \/\/ ok; t is addressable and the compiler rewrites this to (*t).M()\n\n    var m map[string]T\n    m[\"x\"].M() \/\/ error: cannot take the address of m[\"x\"]\n}\n<\/pre>\n<p>\u53e6\u8bf7\u53c2\u9605\uff1a<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5173\u4e8e\u63a5\u53e3\u5206\u914d\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207355","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207355","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=207355"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207355\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}