{"id":208156,"date":"2025-07-08T17:56:59","date_gmt":"2025-07-08T09:56:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208156\/"},"modified":"2025-07-08T17:56:59","modified_gmt":"2025-07-08T09:56:59","slug":"%e6%9c%89%e6%b2%a1%e6%9c%89%e5%8a%9e%e6%b3%95%e5%9c%a8golang%e4%b8%ad%e5%a4%84%e7%90%86nil%e6%8c%87%e9%92%88%e8%80%8c%e4%b8%8d%e4%bd%bf%e7%94%a8if-else%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208156\/","title":{"rendered":"\u6709\u6ca1\u6709\u529e\u6cd5\u5728Golang\u4e2d\u5904\u7406nil\u6307\u9488\u800c\u4e0d\u4f7f\u7528if\/else\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>\u6709\u6ca1\u6709\u529e\u6cd5\u5728Golang\u4e2d\u5904\u7406nil\u6307\u9488\u800c\u4e0d\u4f7f\u7528if\/else\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 17:15: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>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6709\u6ca1\u6709\u529e\u6cd5\u5728Golang\u4e2d\u5904\u7406nil\u6307\u9488\u800c\u4e0d\u4f7f\u7528if\/else\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4f8b\u5982\u6211\u6709\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre>type MyStruct struct {    \n    ...\n    MyField *MyOtherStruct\n    ...\n}\n\ntype MyOtherStruct struct {\n    ...\n    MyOtherField *string\n    ...\n}\n\n\/\/ I have a fuction that receive MyOtherField as parameter\nfunc MyFunc(myOtherField *string) {\n    ...\n}\n\n\/\/ How can I avoid using if\/else here\nif MyStruct.MyField != nil {\n    MyFunc((*MyStruct.MyField).MyOtherField)\n} else {\n    MyFunc(nil)\n}<\/pre>\n<p>\u5728\u6211\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5fc5\u987b\u4f7f\u7528 if else \u6765\u5904\u7406 <code>mystruct.myfield<\/code> \u662f\u5426\u4e3a\u96f6\u3002\u6211\u60f3\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u6765\u7f29\u77ed\u6211\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6211\u60f3\u5728javascript\u4e2d\u627e\u5230\u7c7b\u4f3c <code>myfunc(mystruct.myfield ? (*mystruct.myfield).myotherfield : nil)<\/code> \u7684\u65b9\u6cd5\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4e0d\uff0c\u4f60\u4e0d\u80fd\u505a\u4f60\u4ee5\u524d\u5728 js \u4e2d\u505a\u7684\u4e8b\u60c5\u3002\u5b83\u53ea\u662f\u8bed\u6cd5\u7cd6\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u4e00\u4e9b\u66ff\u4ee3\u65b9\u6848\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u5730\u5199\uff1a<\/p>\n<pre>if mystruct.myfield != nil {\n    myfunc(mystruct.myfield.myotherfield)\n} else {\n    myfunc(nil)\n}<\/pre>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7f16\u5199\u4e0e\u6307\u9488\u63a5\u6536\u5668\u4e00\u8d77\u4f7f\u7528\u7684 getter \u53ef\u80fd\u662f\u6709\u610f\u4e49\u7684\uff1a<\/p>\n<pre>func (m *myotherstruct) getotherfield() *otherfieldtype {\n   if m==nil {\n      return nil\n   }\n   return m.otherfield\n}<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>MyFunc(MyStruct.MyField.GetOtherField())<\/pre>\n<p>\u8fd9\u5c31\u662f grpc \u751f\u6210 go \u6a21\u578b\u7684\u65b9\u5f0f\u3002\u4f46\u8fd9\u901a\u5e38\u662f\u4e0d\u53ef\u53d6\u7684\u3002\u5b83\u9690\u85cf\u4e86\u5fae\u5999\u7684\u9519\u8bef\u3002\u6700\u597d\u660e\u786e\u5e76\u68c0\u67e5\u4f60\u5728\u54ea\u91cc\u4f7f\u7528\u5b83\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u6709\u6ca1\u6709\u529e\u6cd5\u5728Golang\u4e2d\u5904\u7406nil\u6307\u9488\u800c\u4e0d\u4f7f\u7528if\/else\uff1f\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-208156","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208156","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=208156"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208156\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}