{"id":236888,"date":"2020-12-31T12:18:09","date_gmt":"2020-12-31T04:18:09","guid":{"rendered":"http:\/\/4563.org\/?p=236888"},"modified":"2020-12-31T12:18:09","modified_gmt":"2020-12-31T04:18:09","slug":"%e4%b8%80%e4%b8%aa%e6%9e%84%e9%80%a0%e7%bb%93%e6%9e%84%e4%bd%93%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=236888","title":{"rendered":"\u4e00\u4e2a\u6784\u9020\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a\u6784\u9020\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fumeboy <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5047\u8bbe\u8981\u6784\u9020 testStruct \u8fd9\u4e2a\u7ed3\u6784\u4f53, \u5e38\u89c4\u65b9\u6cd5\u662f\u5199\u4e00\u4e2a <code>func NewStruct() testStruct<\/code> \u51fd\u6570<\/p>\n<p>\u8fd9\u91cc\u7684\u65b9\u6cd5\u662f \u5c06\u8fd9\u4e2a\u51fd\u6570 \u6539\u5199\u6210\u7ed3\u6784\u4f53\u65b9\u6cd5 <code>func (s *testStruct) New() *testStruct<\/code> <\/p>\n<p>\u6bd4\u8f83\u4e00\u4e0b\u4f7f\u7528\u65f6\u7684\u4e0d\u540c:<\/p>\n<pre><code>_ = NewStruct()  _ = (&amp;testStruct{}).New()  <\/code><\/pre>\n<p>\u53ef\u4ee5\u53d1\u73b0 New \u8fd9\u4e2a\u51fd\u6570\u88ab\u7ea6\u675f\u5230\u4e86 testStruct \u547d\u540d\u7a7a\u95f4\u4e0b, \u8fd9\u4e5f\u662f\u6211\u7684\u4e3b\u8981\u76ee\u7684<\/p>\n<h3>\u5b8c\u6574\u7684\u6d4b\u8bd5\u7528\u4f8b:<\/h3>\n<p>\u57fa\u51c6\u6d4b\u8bd5\u4e2d, \u4e24\u79cd\u65b9\u5f0f\u7684\u6267\u884c\u6548\u7387\u51e0\u4e4e\u4e00\u81f4,\u5f53 struct \u5f88\u5927\u65f6, <code>(&amp;testStruct{}).New()<\/code>\u7565\u6709\u4f18\u52bf<\/p>\n<pre><code>package main  import (  \"testing\" )  type testStruct struct {  value int }  \/\/ \u4e09\u79cd\u6784\u9020\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5  func NewStruct() *testStruct { \/\/ \u9003\u9038  return &amp;testStruct{} }  func NewStruct2() testStruct { \/\/ \u4e0d\u9003\u9038 \u4f46\u4f20\u503c\u65f6\u53d1\u751f\u62f7\u8d1d  return testStruct{} }  func (s *testStruct) New() *testStruct {   \/\/ \u4e0d\u9003\u9038\uff0c\u53ea\u62f7\u8d1d\u4e86\u6307\u9488  \/\/ \u66f4\u91cd\u8981\u7684\u662f\u6536\u675f\u5230\u4e86 testStruct \u8fd9\u4e2a\u547d\u540d\u7a7a\u95f4\u91cc  return s }    func TestNew(t *testing.T){  _ = (&amp;testStruct{}).New()  \/\/fmt.Println(s) }  func BenchmarkNew(b *testing.B){  for i := 0;i&lt;b.N;i++{   _ = (&amp;testStruct{}).New()  } }  func BenchmarkNew2(b *testing.B){  for i := 0;i&lt;b.N;i++{   _ = NewStruct()  } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4687656\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vincentxue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u79cd\u5199\u6cd5\u662f\u53d7\u4e86 OO \u601d\u60f3\u7684\u5f71\u54cd\uff0c\u6700\u597d\u8fd8\u662f\u4e0d\u8981\u7528 OO \u7684\u601d\u60f3\u53bb\u5199 Go \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4687657\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sxfscool <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u5565\u7528\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4687658\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : siteshen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u300c\u6784\u9020\u51fd\u6570\u300d\u7684\u672c\u8d28\u662f\u300c\u65e0\u4e2d\u751f\u6709\u300d\uff0c\u7b2c\u4e09\u79cd\u662f\u300c\u6709\u4e2d\u751f\u6709\u300d\uff0c\u4e0d\u914d\u79f0\u4f5c\u300c\u6784\u9020\u51fd\u6570\u300d\u3002<\/p>\n<p> func (s *testStruct) New() *testStruct                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4687659\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sthwrong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90fd\u5df2\u7ecf\u6709\u4e86&amp;testStruct{}, \u4e3a\u5565\u8fd8\u8981 New \u4e00\u4e2a\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4687660\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : fumeboy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sthwrong \u521d\u59cb\u5316                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u6784\u9020\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5 \u8cc7\u6df1\u5927\u4f6c :&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\/236888"}],"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=236888"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/236888\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=236888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=236888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=236888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}