{"id":10008317,"date":"2023-09-22T23:54:22","date_gmt":"2023-09-22T15:54:22","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008317"},"modified":"2023-09-22T23:54:22","modified_gmt":"2023-09-22T15:54:22","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-go-%e4%b8%ad%e7%9a%84-new-%e5%92%8c-make-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008317","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Go \u4e2d\u7684 new() \u548c make() \u51fd\u6570"},"content":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<code>new()<\/code> \u548c <code>make()<\/code> \u662f\u4e24\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 <code>new()<\/code> \u548c <code>make()<\/code> \u7684\u533a\u522b\u3001\u4f7f\u7528\u573a\u666f\u4ee5\u53ca\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u3002<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<ul>\n<li>Go \u4e2d\u7684 <code>new()<\/code> \u548c <code>make()<\/code> \u51fd\u6570\u662f\u7528\u4e8e\u521b\u5efa\u548c\u521d\u59cb\u5316\u53d8\u91cf\u7684\u91cd\u8981\u5de5\u5177\u3002<\/li>\n<li><code>new()<\/code> \u7528\u4e8e\u521b\u5efa\u6307\u5b9a\u7c7b\u578b\u7684\u96f6\u503c\u53d8\u91cf\uff0c\u5e76\u8fd4\u56de\u8be5\u53d8\u91cf\u7684\u6307\u9488\u3002<\/li>\n<li><code>make()<\/code> \u7528\u4e8e\u521b\u5efa\u5e76\u521d\u59cb\u5316\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5982\u5207\u7247\u3001\u6620\u5c04\u548c\u901a\u9053\u3002<\/li>\n<\/ul>\n<h2><code>new()<\/code> \u51fd\u6570<\/h2>\n<ul>\n<li><code>new()<\/code> \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u53ca\u7528\u6cd5\u3002<\/li>\n<li><code>new()<\/code> \u521b\u5efa\u7684\u53d8\u91cf\u662f\u6307\u5b9a\u7c7b\u578b\u7684\u96f6\u503c\uff0c\u5e76\u8fd4\u56de\u8be5\u53d8\u91cf\u7684\u6307\u9488\u3002<\/li>\n<li><code>new()<\/code> \u9002\u7528\u4e8e\u521b\u5efa\u5f15\u7528\u7c7b\u578b\u4ee5\u5916\u7684\u5176\u4ed6\u7c7b\u578b\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<pre><code class=\"go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    \/\/ \u4f7f\u7528 new() \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u96f6\u503c\u53d8\u91cf\u7684\u6307\u9488\n    numPtr := new(int)\n\n    fmt.Println(*numPtr) \/\/ \u8f93\u51fa 0\n}\n<\/code><\/pre>\n<h2><code>make()<\/code> \u51fd\u6570<\/h2>\n<ul>\n<li><code>make()<\/code> \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u53ca\u7528\u6cd5\u3002<\/li>\n<li><code>make()<\/code> \u7528\u4e8e\u521b\u5efa\u5e76\u521d\u59cb\u5316\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/li>\n<li><code>make()<\/code> \u9002\u7528\u4e8e\u521b\u5efa\u5207\u7247\u3001\u6620\u5c04\u548c\u901a\u9053\u7b49\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/li>\n<li><code>make()<\/code> \u521b\u5efa\u7684\u53d8\u91cf\u4e0d\u662f\u96f6\u503c\uff0c\u800c\u662f\u6839\u636e\u7c7b\u578b\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<pre><code class=\"go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    \/\/ \u4f7f\u7528 make() \u521b\u5efa\u4e00\u4e2a\u5207\u7247\uff0c\u5e76\u521d\u59cb\u5316\u957f\u5ea6\u4e3a 3 \u7684\u5207\u7247\n    slice := make([]int, 3)\n\n    fmt.Println(slice) \/\/ \u8f93\u51fa [0 0 0]\n}\n<\/code><\/pre>\n<h2><code>new()<\/code> \u548c <code>make()<\/code> \u7684\u533a\u522b<\/h2>\n<ul>\n<li><code>new()<\/code> \u7528\u4e8e\u521b\u5efa\u4efb\u610f\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u800c <code>make()<\/code> \u4ec5\u7528\u4e8e\u521b\u5efa\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/li>\n<li><code>new()<\/code> \u8fd4\u56de\u7684\u662f\u6307\u9488\uff0c\u800c <code>make()<\/code> \u8fd4\u56de\u7684\u662f\u521d\u59cb\u5316\u540e\u7684\u503c\u3002<\/li>\n<li><code>new()<\/code> \u521b\u5efa\u7684\u53d8\u91cf\u662f\u96f6\u503c\uff0c<code>make()<\/code> \u521b\u5efa\u7684\u53d8\u91cf\u662f\u6839\u636e\u7c7b\u578b\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<pre><code class=\"go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    \/\/ \u4f7f\u7528 new() \u521b\u5efa\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u6307\u9488\n    personPtr := new(Person)\n    personPtr.Name = &quot;Alice&quot;\n    personPtr.Age = 30\n\n    fmt.Println(personPtr) \/\/ \u8f93\u51fa &amp;{Alice 30}\n\n    \/\/ \u4f7f\u7528 make() \u521b\u5efa\u4e00\u4e2a\u6620\u5c04\uff0c\u5e76\u521d\u59cb\u5316\u952e\u503c\u5bf9\n    m := make(map[string]int)\n    m[&quot;one&quot;] = 1\n    m[&quot;two&quot;] = 2\n\n    fmt.Println(m) \/\/ \u8f93\u51fa map[one:1 two:2]\n}\n\ntype Person struct {\n    Name string\n    Age  int\n}\n<\/code><\/pre>\n<h2><code>new()<\/code> \u548c <code>make()<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<code>new()<\/code> \u548c <code>make()<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u7565\u6709\u4e0d\u540c\u3002<\/p>\n<h3><code>new()<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406<\/h3>\n<ul>\n<li><code>new()<\/code> \u51fd\u6570\u5728\u5e95\u5c42\u4f7f\u7528\u4e86 Go \u7684 <code>runtime.newobject<\/code> \u51fd\u6570\u3002<\/li>\n<li><code>runtime.newobject<\/code> \u51fd\u6570\u4f1a\u5206\u914d\u4e00\u5757\u5185\u5b58\uff0c\u5927\u5c0f\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5185\u5b58\u6e05\u96f6\u3002<\/li>\n<li>\u7136\u540e\uff0c<code>runtime.newobject<\/code> \u51fd\u6570\u4f1a\u8fd4\u56de\u8fd9\u5757\u5185\u5b58\u7684\u6307\u9488\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f <code>new()<\/code> \u51fd\u6570\u7684\u7b80\u5316\u7248\u672c\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;unsafe&quot;\n)\n\nfunc main() {\n    \/\/ \u4f7f\u7528 new() \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u96f6\u503c\u53d8\u91cf\u7684\u6307\u9488\n    numPtr := new(int)\n\n    \/\/ \u83b7\u5f97\u6307\u9488\u7684\u503c\n    ptrValue := uintptr(unsafe.Pointer(numPtr))\n\n    \/\/ \u8f93\u51fa\u6307\u9488\u7684\u503c\n    fmt.Println(ptrValue)\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>unsafe<\/code> \u5305\u4e2d\u7684 <code>Pointer<\/code> \u548c <code>uintptr<\/code> \u7c7b\u578b\u6765\u64cd\u4f5c\u6307\u9488\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>new(int)<\/code> \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u96f6\u503c\u53d8\u91cf\u7684\u6307\u9488 <code>numPtr<\/code>\uff0c\u7136\u540e\u901a\u8fc7 <code>unsafe.Pointer<\/code> \u5c06\u6307\u9488\u8f6c\u6362\u4e3a <code>unsafe.Pointer<\/code> \u7c7b\u578b\uff0c\u518d\u901a\u8fc7 <code>uintptr<\/code> \u5c06 <code>unsafe.Pointer<\/code> \u503c\u8f6c\u6362\u4e3a <code>uintptr<\/code> \u7c7b\u578b\uff0c\u6700\u540e\u8f93\u51fa\u6307\u9488\u7684\u503c\u3002\u8fd9\u4e2a\u503c\u5c31\u662f\u6211\u4eec\u6240\u521b\u5efa\u7684\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<h3><code>make()<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406<\/h3>\n<ul>\n<li><code>make()<\/code> \u51fd\u6570\u5728\u5e95\u5c42\u4f7f\u7528\u4e86 Go \u7684 <code>runtime.makeslice<\/code>\u3001<code>runtime.makemap<\/code> \u548c <code>runtime.makechan<\/code> \u51fd\u6570\u3002<\/li>\n<li><code>runtime.makeslice<\/code> \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5207\u7247\uff0c\u5b83\u4f1a\u5206\u914d\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5e76\u8fd4\u56de\u5207\u7247\u7ed3\u6784\u4f53\u3002<\/li>\n<li><code>runtime.makemap<\/code> \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6620\u5c04\uff0c\u5b83\u4f1a\u5206\u914d\u4e00\u5757\u54c8\u5e0c\u8868\u5185\u5b58\uff0c\u5e76\u8fd4\u56de\u6620\u5c04\u7ed3\u6784\u4f53\u3002<\/li>\n<li><code>runtime.makechan<\/code> \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u901a\u9053\uff0c\u5b83\u4f1a\u5206\u914d\u4e00\u5757\u901a\u9053\u5185\u5b58\uff0c\u5e76\u8fd4\u56de\u901a\u9053\u7ed3\u6784\u4f53\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f <code>make()<\/code> \u51fd\u6570\u7684\u7b80\u5316\u7248\u672c\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;reflect&quot;\n    &quot;unsafe&quot;\n)\n\nfunc main() {\n    \/\/ \u4f7f\u7528 make() \u521b\u5efa\u4e00\u4e2a\u5207\u7247\uff0c\u5e76\u521d\u59cb\u5316\u957f\u5ea6\u4e3a 3 \u7684\u5207\u7247\n    slice := make([]int, 3)\n\n    \/\/ \u83b7\u5f97\u5207\u7247\u7684\u503c\u548c\u957f\u5ea6\n    sliceValue := reflect.ValueOf(slice)\n    sliceData := sliceValue.Elem().UnsafeAddr()\n    sliceLen := sliceValue.Len()\n\n    \/\/ \u8f93\u51fa\u5207\u7247\u7684\u503c\u548c\u957f\u5ea6\n    fmt.Println(sliceData, sliceLen)\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>reflect<\/code> \u5305\u4e2d\u7684 <code>Value<\/code>\u3001<code>Elem<\/code> \u548c <code>UnsafeAddr<\/code> \u65b9\u6cd5\u6765\u64cd\u4f5c\u5207\u7247\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>make([]int, 3)<\/code> \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a 3 \u7684\u5207\u7247 <code>slice<\/code>\uff0c\u7136\u540e\u901a\u8fc7 <code>reflect.ValueOf<\/code> \u5c06\u5207\u7247\u8f6c\u6362\u4e3a <code>reflect.Value<\/code> \u7c7b\u578b\uff0c\u518d\u901a\u8fc7 <code>Elem<\/code> \u65b9\u6cd5\u83b7\u53d6\u5207\u7247\u7684\u5143\u7d20\uff0c\u5e76\u901a\u8fc7 <code>UnsafeAddr<\/code> \u65b9\u6cd5\u83b7\u53d6\u5207\u7247\u7684\u5e95\u5c42\u6570\u7ec4\u7684\u6307\u9488\uff0c\u6700\u540e\u901a\u8fc7 <code>Len<\/code> \u65b9\u6cd5\u83b7\u53d6\u5207\u7247\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u83b7\u5f97\u5207\u7247\u7684\u5e95\u5c42\u6570\u7ec4\u7684\u6307\u9488\u548c\u957f\u5ea6\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86 <code>reflect<\/code> \u548c <code>unsafe<\/code> \u5305\uff0c\u8fd9\u662f\u4e3a\u4e86\u6f14\u793a <code>make()<\/code> \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u800c\u5f15\u5165\u7684\uff0c\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e76\u4e0d\u9700\u8981\u7ecf\u5e38\u4f7f\u7528\u8fd9\u4e9b\u5305\u3002<\/p>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<p>\u901a\u8fc7\u6df1\u5165\u4e86\u89e3 <code>new()<\/code> \u548c <code>make()<\/code> \u51fd\u6570\u7684\u533a\u522b\u3001\u4f7f\u7528\u573a\u666f\u4ee5\u53ca\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff0c\u8bfb\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u8fd0\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u5e76\u5b8c\u7f8e\u89e3\u51b3\u6389\u9762\u8bd5\u5b98\u7684\u95ee\u9898\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u505a\u51fa\u51c6\u786e\u7684\u9009\u62e9\u3002<\/p>\n<p>\u672c\u6587\u7531<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=lmhmBe8Sd%2BXC9t%2FHYZPaNg%3D%3D.t%2BemGj9hI09AwltXopKB3Ey0pzxS0aX%2F0wFFMRN4RDo%3D\" rel=\"noopener noreferrer\">mdnice<\/a>\u591a\u5e73\u53f0\u53d1\u5e03<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044249084\">https:\/\/segmentfault.com\/a\/1190000044249084<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0cnew() \u548c make() \u662f\u4e24\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u521d\u59cb\u5316\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008317","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008317"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008317\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}