{"id":165301,"date":"2020-10-03T20:24:19","date_gmt":"2020-10-03T12:24:19","guid":{"rendered":"http:\/\/4563.org\/?p=165301"},"modified":"2020-10-03T20:24:19","modified_gmt":"2020-10-03T12:24:19","slug":"go-%e8%af%ad%e8%a8%80%e8%bf%99%e7%a7%8d%e7%bb%93%e6%9e%84%e4%bd%93%e8%b0%83%e7%94%a8%e6%96%b9%e6%b3%95%e7%9a%84%e5%86%99%e6%b3%95%e6%98%af%e6%80%8e%e4%b9%88%e5%9b%9e%e4%ba%8b%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=165301","title":{"rendered":"Go \u8bed\u8a00\u8fd9\u79cd\u7ed3\u6784\u4f53\u8c03\u7528\u65b9\u6cd5\u7684\u5199\u6cd5\u662f\u600e\u4e48\u56de\u4e8b\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Go \u8bed\u8a00\u8fd9\u79cd\u7ed3\u6784\u4f53\u8c03\u7528\u65b9\u6cd5\u7684\u5199\u6cd5\u662f\u600e\u4e48\u56de\u4e8b\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakono <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u65e0\u610f\u95f4\u53d1\u73b0\u7684\u4e00\u4e2a\u5199\u6cd5<\/p>\n<pre><code>type T struct {  t int }  func (t T) NewT(tValue int) T {  return T{t: tValue}  } func main() {  t := T.NewT(T{},999)  fmt.Println(t) }   <\/code><\/pre>\n<p>\u8f93\u51fa {999}<\/p>\n<p>\u6ca1\u6709\u7ed3\u6784\u4f53\u5b9e\u4f8b\uff0c\u4f46\u662f\u5374\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7c7b\u578b\u6765\u8c03\u7528\u5bf9\u5e94\u7684\u65b9\u6cd5<\/p>\n<pre><code>t := T.NewT(T{},999) <\/code><\/pre>\n<p>\u8fd9\u5199\u6cd5\u6709\u70b9\u50cf\u8c03\u7528\u9759\u6001\u65b9\u6cd5<\/p>\n<p>\u901a\u8fc7\u4e00\u4e9b\u6d4b\u8bd5\u53ef\u4ee5\u77e5\u9053\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684 T{} \u5fc5\u987b\u5b58\u5728\uff0c\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f1a\u88ab\u5f53\u6210\u65b9\u6cd5\u7684 reciver \u4f20\u5165\u65b9\u6cd5\u4e2d\uff08\u83ab\u975e go \u8bed\u8a00\u662f\u901a\u8fc7\u8fd9\u79cd\u673a\u5236\u5b9e\u73b0\u7684\u65b9\u6cd5\u8c03\u7528\uff09 \u5982\u679c\u65b9\u6cd5\u4e0d\u53bb\u7ba1\u4f20\u5165\u7684 reciver \u7684\u8bdd\uff0c\u8fd9\u79cd\u5199\u6cd5\u4f3c\u4e4e\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u7684\u5199\u6cd5\uff1f<\/p>\n<p>\u7ffb\u4e86\u4e0b\u4f3c\u4e4e\u6ca1\u627e\u5230\u8fd9\u79cd\u5199\u6cd5\u7684\u8bf4\u660e\uff0c\u4e0d\u77e5\u9053\u5b98\u7f51\u6709\u6ca1\u6709\u5bf9\u5e94\u7684\u5730\u65b9\u4ecb\u7ecd\uff08\u4e5f\u53ef\u80fd\u662f\u6211\u6ca1\u627e\u5bf9\u5730\u65b9\uff09<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3641801\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cmdOptionKana <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/golang.org\/ref\/spec<\/p>\n<p>That function may be called normally with an explicit receiver, so these five invocations are equivalent:<br \/>t.Mv(7)<br \/>T.Mv(t, 7)<br \/>(T).Mv(t, 7)<br \/>f1 := T.Mv; f1(t, 7)<br \/>f2 := (T).Mv; f2(t, 7)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641802\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SingeeKing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Python \u548c rust \u90fd\u662f\u8fd9\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641803\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65b9\u6cd5\u672c\u6765\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f receiver\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528\u8fd9\u4e2a\u65b9\u5f0f\u8c03\u7528                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641804\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : littlewing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             t := T.NewT(T{},999)<br \/>\u7b49\u4ef7\u4e8e<br \/>t := T{}.NewT(999)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641805\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u867d\u7136\u80fd\u8dd1\uff0c\u4f46\u662f\u8fd9\u79cd\u5199\u6cd5\u4e0d\u592a\u6b63\u89c4\u3002<br \/>t := T.NewT(T{},999)<br \/>\u8fd9\u53e5\u7684 T{}\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u4ee5\u4f20\u503c\u65b9\u5f0f\u4f20\u7ed9 NewT \u51fd\u6570\u3002\u7136\u540e NewT \u51fd\u6570\u5b8c\u5168\u6ca1\u7528\u5230\u4f20\u8fdb\u6765\u7684\u8fd9\u4e2a\u5bf9\u8c61\uff0c<br \/>\u800c\u662f\u5728 return \u7684\u65f6\u5019\u53c8\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61 T{t: tValue}\uff0c<br \/>\u7136\u540e\u56de\u5230 main()\u51fd\u6570\uff0c\u5b83\u53c8\u4ee5\u590d\u5236\u7684\u65b9\u5f0f\u8d4b\u7ed9 t \u53d8\u91cf(\u8fd9\u53c8\u662f\u53e6\u4e00\u4e2a\u5bf9\u8c61)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641806\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @user8341 \u5982\u679c receiver \u662f T\uff0c\u90a3\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5c31\u662f\u590d\u5236\u518d\u4f20\u53c2\u7684\u3002\u6240\u6709\u53c2\u6570\u90fd\u662f\u590d\u5236\u518d\u4f20\u9012\u7684\u3002\u4e0d\u8981\u4ee5\u4e3a\u7528\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\uff0c\u5c31\u4e0d\u4f1a\u590d\u5236\u3002T \u548c*T \u4e24\u79cd receiver \u662f\u6709\u533a\u522b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641807\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunriz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c03\u7528 type.method \u5b9e\u9645\u4e0a\u5c31\u662f\u628a method \u7684 receiver \u66b4\u9732\u4e3a\u53c2\u6570\u4e86<br \/>\u4e00\u822c\u5c31\u8fd9\u4e24\u79cd\uff1a<br \/>1. type.method(type{},&#8230;)<br \/>2. (*type).method(new(type),&#8230;)<br \/>\u770b method \u600e\u4e48\u7684\u5b9a\u4e49\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3641808\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reus \u6211\u7684\u7b54\u6848\u4f60\u6ca1\u770b\u61c2\u3002\u4e0d\u8fc7\u8c22\u8c22\u60a8\u7684\u8865\u5145\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u8fd9\u79cd\u7ed3\u6784\u4f53\u8c03\u7528\u65b9\u6cd5\u7684\u5199\u6cd5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/165301"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165301"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/165301\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165301"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}