{"id":901,"date":"2024-11-07T16:02:54","date_gmt":"2024-11-07T08:02:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/901\/"},"modified":"2024-11-07T16:02:54","modified_gmt":"2024-11-07T08:02:54","slug":"go-%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84-var-_-type-typenil-%e8%af%ad%e6%b3%95%e6%9c%89%e4%bb%80%e4%b9%88%e4%bd%9c%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/901\/","title":{"rendered":"Go \u8bed\u8a00\u4e2d\u7684 `var _ Type = (*type)(nil)` \u8bed\u6cd5\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1003\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173055334014054.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173055334014054.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173055334014054-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173055334014054-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173055334014054-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Go \u8bed\u8a00\u4e2d\u7684 `var _ Type = (*type)(nil)` \u8bed\u6cd5\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f\u63d2\u56fe\" alt=\"Go \u8bed\u8a00\u4e2d\u7684 `var _ Type = (*type)(nil)` \u8bed\u6cd5\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>go \u8bed\u8a00\u4e2d\u4e00\u4e2a\u7279\u6b8a\u8bed\u6cd5\uff1avar _ type = (*type)(nil)<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u6709\u4e00\u884c\u8bed\u6cd5\u53ef\u80fd\u4f1a\u8ba9\u65b0\u624b\u611f\u5230\u56f0\u60d1\uff1avar _ type = (*type)(nil)\u3002\u90a3\u4e48\uff0c\u8fd9\u884c\u4ee3\u7801\u5230\u5e95\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f<\/p>\n<p><strong>\u8bed\u6cd5\u8be6\u89e3<\/strong><\/p>\n<ul>\n<li>var _ type\uff1a\u58f0\u660e\u4e00\u4e2a\u540d\u4e3a _ \u7684\u53d8\u91cf\uff0c\u7c7b\u578b\u4e3a type\u3002\u7531\u4e8e _ \u53ea\u662f\u5360\u4f4d\u7b26\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u6216\u5206\u914d\u3002<\/li>\n<li>(*type)(nil)\uff1a\u5c06 type \u7684\u96f6\u503c\uff08\u5373 nil\uff09\u8f6c\u6362\u4e3a type \u6307\u9488\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u9014<\/strong><\/p>\n<p>\u8fd9\u884c\u4ee3\u7801\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4f5c\u4e3a\u7f16\u8bd1\u65f6\u7684\u7c7b\u578b\u9632\u5b88\u673a\u5236\u3002\u5b83\u7528\u4e8e\u68c0\u67e5 type \u662f\u5426\u6b63\u786e\u5b9e\u73b0\u4e86 type \u63a5\u53e3\u3002\u5982\u679c\u5b9e\u73b0\u4e0d\u6b63\u786e\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u7f16\u8bd1\u65f6\u62a5\u9519\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u4ee5\u793a\u4f8b\u6765\u8bf4\u660e\u5b83\u7684\u7528\u6cd5\uff1a<\/p>\n<pre>var _ hellointer = (*cat)(nil)\n\ntype hellointer interface {\n    hello() string\n}\n\ntype cat struct {\n    hellointer\n    name string\n}\n\nfunc (c *cat) hello() string {\n    return c.name + \"miaomiao\"\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 var _ type = (*type)(nil) \u6765\u786e\u4fdd cat \u7c7b\u578b\u5b9e\u73b0\u4e86 hellointer \u63a5\u53e3\u3002\u5982\u679c cat \u7c7b\u578b\u6ca1\u6709\u5b9e\u73b0\u8be5\u63a5\u53e3\uff0c\u7f16\u8bd1\u5668\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>invalid conversion: (*Cat)(nil) is not HelloInter<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u4f7f\u7528 var _ type = (*type)(nil) \u662f\u5728\u7f16\u8bd1\u65f6\u786e\u4fdd\u7c7b\u578b\u6b63\u786e\u6027\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5728\u4ee3\u7801\u5f00\u53d1\u65e9\u671f\u53d1\u73b0\u5b9e\u73b0\u95ee\u9898\uff0c\u4ece\u800c\u907f\u514d\u8fd0\u884c\u65f6\u7684\u4e0d\u53ef\u9884\u6599\u884c\u4e3a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo \u8bed\u8a00\u4e2d\u7684 `var _ Type = (*type)(nil)` \u8bed\u6cd5\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go \u8bed\u8a00\u4e2d\u4e00\u4e2a\u7279\u6b8a\u8bed\u6cd5\uff1avar _ type = (*type)(nil) \u5728 go \u8bed\u8a00\u4e2d\uff0c\u6709\u4e00\u884c\u8bed\u6cd5\u53ef\u80fd\u4f1a\u8ba9\u65b0\u624b\u611f\u5230\u56f0\u60d1\uff1avar _ type = (*type)(nil)\u3002\u90a3\u4e48\uff0c\u8fd9\u884c\u4ee3\u7801\u5230\u5e95\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f \u8bed\u6cd5\u8be6\u89e3 var _ type\uff1a\u58f0\u660e\u4e00\u4e2a\u540d\u4e3a _ \u7684\u53d8\u91cf\uff0c\u7c7b\u578b\u4e3a type\u3002\u7531\u4e8e _ \u53ea\u662f\u5360\u4f4d\u7b26\uff0c\u56e0\u6b64\u5b83\u4e0d\u4f1a\u88ab\u5b9e\u9645\u4f7f\u7528\u6216\u5206\u914d\u3002 (*type)(nil)\uff1a\u5c06 type \u7684\u96f6\u503c\uff08\u5373 nil\uff09\u8f6c\u6362\u4e3a type \u6307\u9488\u7c7b\u578b\u3002 \u7528\u9014 \u8fd9\u884c\u4ee3\u7801\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4f5c\u4e3a\u7f16\u8bd1\u65f6\u7684\u7c7b\u578b\u9632\u5b88\u673a\u5236\u3002\u5b83\u7528\u4e8e\u68c0\u67e5 type \u662f\u5426\u6b63\u786e\u5b9e\u73b0\u4e86 type \u63a5\u53e3\u3002\u5982\u679c\u5b9e\u73b0\u4e0d\u6b63\u786e\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u7f16\u8bd1\u65f6\u62a5\u9519\u3002 \u793a\u4f8b \u6211\u4eec\u4ee5\u793a\u4f8b\u6765\u8bf4\u660e\u5b83\u7684\u7528\u6cd5\uff1a var _ hellointer = (*cat)(nil) type hellointer interface { hello() string } type cat struct { hellointer name string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-901","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/901","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=901"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/901\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}