{"id":165,"date":"2021-09-02T09:29:32","date_gmt":"2021-09-02T01:29:32","guid":{"rendered":"https:\/\/chenqinghe.com\/?p=165"},"modified":"2021-09-02T09:34:24","modified_gmt":"2021-09-02T01:34:24","slug":"%e6%96%b9%e6%b3%95%e7%9a%84receiver%e4%b8%banil%e7%9a%84%e6%97%b6%e5%80%99%e6%98%af%e5%90%a6%e4%bc%9a%e9%80%a0%e6%88%90panic%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/chenqinghe.com\/?p=165","title":{"rendered":"\u65b9\u6cd5\u7684receiver\u4e3anil\u7684\u65f6\u5019\u662f\u5426\u4f1a\u9020\u6210panic\uff1f"},"content":{"rendered":"<p>\u4ee5\u4e0b\u4ee3\u7801\u662f\u5426\u4f1a\u9020\u6210\u7a0b\u5e8fpanic\uff1f<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport \"fmt\"\n\ntype Table struct {\n\tname string \n}\n\nfunc (t *Table) TableName() string {\n\treturn t.name\n}\n\nfunc main() {\n\n\tvar table *Table\n\n\tfmt.Println(table.TableName())\n\n}<\/pre>\n<p>\u6beb\u65e0\u7591\u95ee\uff0c<strong>\u4f1a<\/strong>\uff01<\/p>\n<p>\u56e0\u4e3atable\u53ea\u662f\u58f0\u660e\u4e86\u53d8\u91cf\uff0c\u4f46\u662f\u6ca1\u6709\u8d4b\u503c\uff0c\u6b64\u65f6\u503c\u4e3a\u96f6\u503cnil\uff0c\u5728\u8c03\u7528\u5bf9\u5e94\u65b9\u6cd5\u65f6\u4f1a\u9020\u6210nil dereference panic\u3002<\/p>\n<p>\u90a3\u4e0b\u9762\u7684\u4ee3\u7801\u4f1apanic\u5417\uff1f<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport \"fmt\"\n\ntype Table struct {}\n\nfunc (t *Table) TableName() string {\n\treturn \"hello_world\"\n}\n\nfunc main() {\n\n\tvar table *Table\n\n\tfmt.Println(table.TableName())\n\n}<\/pre>\n<p>\u7b54\u6848\u662f\uff0c<strong>\u4e0d\u4f1a<\/strong>\uff01<\/p>\n<p>\u4e3a\u5565\u5462\uff1f\u56e0\u4e3aTableName\u65b9\u6cd5\u91cc\uff0c\u6ca1\u6709\u8bbf\u95eeTable\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u6b64\u4e0d\u6d89\u53ca\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u4e0d\u4f1apanic\u3002<\/p>\n<p>\u5176\u5b9e<strong>\u4e00\u4e2a\u65b9\u6cd5\uff08method\uff09\u7b49\u4ef7\u4e8e\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3areceiver\u7684\u51fd\u6570\uff08function\uff09<\/strong>\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Table struct {}\n\nfunc (t *Table) TableName() string {\n\treturn \"hello_world\"\n}<\/pre>\n<p>\u5176\u5b9e\u7b49\u4ef7\u4e8e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Table struct {}\n\nfunc  TableName(t *Table) string {\n\treturn \"hello_world\"\n}\n<\/pre>\n<p>\u8fd9\u65f6\u5019\u518d\u770b\uff0c\u5c31\u5f88\u660e\u767d\u4e86\uff0c\u4f5c\u4e3a\u53c2\u6570\u7684\u65f6\u5019\uff0ct\u53ef\u4ee5\u4e3anil\u5417\uff1f\u53ef\u4ee5\u3002\u4f1apanic\u5417\uff1f\u4e0d\u4f1a\u3002\u56e0\u6b64\u7b49\u4ef7\u7684\u65b9\u6cd5\u4e5f\u4e0d\u4f1apanic\u3002<\/p>\n<p>\u518d\u4e00\u4e2a\uff0c\u771f\u6b63\u5f15\u8d77panic\u7684\u662f\u5bf9nil\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u64cd\u4f5c\uff0c\u5982\u679c\u6ca1\u6709\u89e3\u5f15\u7528\uff0c\u90a3\u4e48\u4e5f\u4e0d\u4f1a\u9020\u6210panic\u3002\u89e3\u5f15\u7528\u5c31\u662f\u53bb\u5bfb\u627e\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u5bf9\u5e94\u7684\u771f\u6b63\u6570\u636e\uff0c\u5728\u627e\u4e4b\u524d\u4f1a\u5224\u65ad\u5730\u5740\u662f\u5426\u6709\u6548\uff0c\u82e5\u65e0\u6548\uff0c\u5219\u5c31\u4f1a\u5f15\u8d77panic\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Pointer struct{}\n\nfunc (p *Pointer)PrintAddr() {\n\tfmt.Sprint(\"addr: %p\", p)\n}\n\n\/\/ output: addr: 0x0<\/pre>\n<p>\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u867d\u7136\u8bbf\u95ee\u4e86p\uff0c\u5e76\u4e14p\u4e5f\u662fnil\uff0c\u4f46\u662f\u56e0\u4e3a\u6ca1\u6709\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u6ca1\u6709panic\u3002<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ee5\u4e0b\u7684\u4ee3\u7801\u4f1a\u9020\u6210panic\u5417\uff1f<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport \"fmt\"\n\ntype Table struct {}\n\nfunc (t Table) TableName() string {\n\treturn \"hello_world\"\n}\n\nfunc main() {\n\n\tvar table *Table\n\n\tfmt.Println(table.TableName())\n\n}<\/pre>\n<p>\u7b54\u6848\u662f\uff1a<strong>\u4f1a\uff01<\/strong><\/p>\n<p>\u8fd9\u91cc\u4e3a\u5565\u53c8\u4f1a\u4e86\u5462\uff1f<\/p>\n<p>\u6309\u7167\u4e0a\u9762\u7b49\u4ef7\u7406\u8bba\u6765\u8bf4\uff0c\u786e\u5b9e\u662f\u4e0d\u4f1a\uff0c\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c<strong>\u4ee5\u6307\u9488\u7c7b\u578b\u53bb\u8c03\u7528\u975e\u6307\u9488\u7c7b\u578b\u65b9\u6cd5\u65f6\uff0c\u662f\u4f1a\u81ea\u52a8\u5bf9\u6307\u9488\u8fdb\u884c\u89e3\u5f15\u7528\u7684<\/strong>\uff0c\u56e0\u6b64\u8fd8\u662f\u4f1a\u9020\u6210panic\u3002\u7528\u4ee5\u4e0a\u7b49\u4ef7\u7406\u8bba\u89e3\u91ca\u5c31\u662f\u7b49\u4ef7\u4e8e\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Table struct {}\n\nfunc TableName(t Table) string {\n\treturn \"hello_world\"\n}\n\nfunc main() {\n\n\tvar pointer *Table\n\ttable = *pointer\n\tfmt.Println(TableName(table))\n\n}<\/pre>\n<p>\u771f\u6b63\u5bfc\u81f4panic\u7684\u5730\u65b9\u662f\u5728main\u4e2d\u7684\u7b2c\u4e8c\u884c\u4ee3\u7801\uff0c\u800c\u5e76\u975e\u662f\u5728TableName\u4e2d\u3002<\/p>\n<p>\u603b\u7ed3\uff0cnil receiver\u662f\u5426\u4f1a\u9020\u6210panic\uff0c<strong>\u4e3b\u8981\u53d6\u51b3\u4e8e\u662f\u5426\u5bf9\u5176\u8fdb\u884c\u4e86\u89e3\u5f15\u7528\u64cd\u4f5c\uff0c\u5982\u679c\u6709\uff0c\u5219\u4f1apanic\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4e0d\u4f1a\u5f15\u8d77panic<\/strong>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u4ee3\u7801\u662f\u5426\u4f1a\u9020\u6210\u7a0b\u5e8fpanic\uff1f \u6beb\u65e0\u7591\u95ee\uff0c\u4f1a\uff01 \u56e0\u4e3atable\u53ea\u662f\u58f0\u660e\u4e86\u53d8\u91cf\uff0c\u4f46\u662f\u6ca1\u6709\u8d4b\u503c\uff0c\u6b64\u65f6\u503c\u4e3a\u96f6\u503cnil\uff0c\u5728\u8c03\u7528\u5bf9\u5e94\u65b9\u6cd5\u65f6\u4f1a\u9020\u6210nil dereference panic\u3002 \u90a3\u4e0b\u9762\u7684\u4ee3\u7801\u4f1apanic\u5417\uff1f \u7b54\u6848\u662f\uff0c\u4e0d\u4f1a\uff01 \u4e3a\u5565\u5462\uff1f\u56e0\u4e3aTableName\u65b9\u6cd5\u91cc\uff0c\u6ca1\u6709\u8bbf\u95eeTable\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u6b64\u4e0d\u6d89\u53ca\u89e3\u5f15\u7528\uff0c\u6240\u4ee5\u4e0d\u4f1apanic\u3002\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts\/165"}],"collection":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":0,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=\/wp\/v2\/media\/166"}],"wp:attachment":[{"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chenqinghe.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}