{"id":207807,"date":"2025-07-08T08:24:26","date_gmt":"2025-07-08T00:24:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207807\/"},"modified":"2025-07-08T08:24:26","modified_gmt":"2025-07-08T00:24:26","slug":"%e5%9c%a8-struct-%e5%b1%9e%e6%80%a7%e4%b8%ad%e6%8d%95%e8%8e%b7-nil-%e6%8c%87%e9%92%88%e5%8f%96%e6%b6%88%e5%bc%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207807\/","title":{"rendered":"\u5728 Struct \u5c5e\u6027\u4e2d\u6355\u83b7 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528"},"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>\u5728 Struct \u5c5e\u6027\u4e2d\u6355\u83b7 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 18:54:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5728 Struct \u5c5e\u6027\u4e2d\u6355\u83b7 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u4f5c\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u5730\u5740\u7684\u7ed3\u6784\u4f53\u7684\u5c5e\u6027\u65f6\uff0c\u6211\u8bd5\u56fe\u627e\u5230\u6355\u83b7 <code>nil \u6307\u9488\u89e3\u5f15\u7528 <\/code> \u7684\u6700\u4f73\u65b9\u6cd5\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u8fd9\u4e9b\u7ed3\u6784\uff08\u4ee3\u7801\u4ec5\u4f9b\u6f14\u793a\u3002\u6211\u7684\u76ee\u7684\u662f\u4f20\u8fbe\u4e00\u4e2a\u89c2\u70b9\uff09<\/p>\n<pre>type location struct {\n  coordinates *coordinates\n}\n\ntype coordinates struct {\n  lat *latitude\n  lon *longitude\n}\n\ntype latitude struct {\n  lat float64\n}\n\ntype longitude struct {\n  lon float64\n}<\/pre>\n<p>\u521d\u59cb\u5316\u7a7a\u4f4d\u7f6e\u5e76\u8bbf\u95ee <code>loc.coordinates.lat<\/code> \u663e\u7136\u4f1a\u4ea7\u751f\u9884\u671f\u7684\u9519\u8bef<\/p>\n<pre>loc := location{}\nfmt.println(loc.coordinates.lat) \/\/ runtime error: invalid memory address or nil pointer dereference<\/pre>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6211\u53ef\u4ee5\u505a<\/p>\n<pre>if loc.coordinates != nil {\n  fmt.println(loc.coordinates.lat)\n}<\/pre>\n<p>\u4f46\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u60f3\u6253\u5370\u51fa <code>latitude<\/code> \u7684 <code>lat<\/code> \u5c5e\u6027\uff0c\u6211\u5fc5\u987b\u6dfb\u52a0\u53e6\u4e00\u4e2a <code>if<\/code> \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>if loc.coordinates != nil {\n    if(loc.coordinates.lat != nil){\n          fmt.Println(loc.coordinates.lat.lat)\n    }\n  }<\/pre>\n<p>\u6211\u60f3\u77e5\u9053\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u800c\u4e0d\u68c0\u67e5\u6bcf\u4e2a\u5730\u5740\u662f\u5426\u4e0d\u7b49\u4e8e <code>nil<\/code>\u3002 go \u4e2d\u662f\u5426\u6709\u50cf <code>val, ok := somemap[\"foo\"]<\/code> \u8fd9\u6837\u7684\u7ed3\u6784\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u6307\u9488\u7c7b\u578b\uff0c\u4f60\u5fc5\u987b\u5904\u7406\u5b83\u4eec\u53ef\u80fd\u4e3a\u96f6\u7684\u60c5\u51b5\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u68c0\u67e5\u6bcf\u4e2a\u8bbf\u95ee\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u53ef\u4ee5\u5904\u7406 nil \u63a5\u6536\u5668\u7684 getter\uff1a<\/p>\n<pre>func (c *Coordinates) GetLat() (Latitude,bool) {\n  if c==nil {\n    return Latitude{}, false\n  }\n  return c.lat,true\n}\n\nfunc (l *Location) GetCoordinates() *Coordinates {\n   if l==nil {\n      return nil\n   }\n   return l.coordinates\n}\n\nlat, ok:=l.GetCoordinates().GetLat()\nif ok {\n   \/\/ there is a valid lat\n}<\/pre>\n<p>\u4e0d\uff0c\u6ca1\u6709\u3002 \uff08\u4ece\u751f\u6210\u7684\u6050\u614c\u4e2d\u6062\u590d\u662f\u4e00\u79cd\u4e0d\u597d\u7684\u505a\u6cd5\uff0c\u5bb9\u6613\u51fa\u9519\u4e14\u901f\u5ea6\u7f13\u6162\u3002\uff09<\/p>\n<p>\u6ca1\u6709\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728 Struct \u5c5e\u6027\u4e2d\u6355\u83b7 nil \u6307\u9488\u53d6\u6d88\u5f15\u7528\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-207807","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207807","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=207807"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207807\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}