{"id":86,"date":"2025-08-29T16:47:15","date_gmt":"2025-08-29T08:47:15","guid":{"rendered":"https:\/\/www.cloudnote.cc\/?p=86"},"modified":"2025-08-29T16:51:15","modified_gmt":"2025-08-29T08:51:15","slug":"%e3%80%90golang%e3%80%91new%e4%b8%8emake%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.cloudnote.cc\/index.php\/2025\/08\/29\/%e3%80%90golang%e3%80%91new%e4%b8%8emake%e5%8c%ba%e5%88%ab\/","title":{"rendered":"\u3010Golang\u3011new\u4e0emake\u533a\u522b"},"content":{"rendered":"\n<p>Go\u8bed\u8a00\u4e2dnew\u548cmake\u90fd\u662f\u7528\u6765\u5185\u5b58\u5206\u914d\u7684\u539f\u8bed\uff08allocation primitives\uff09\u3002\u7b80\u5355\u7684\u8bf4\uff0cnew\u53ea\u5206\u914d\u5185\u5b58\uff0cmake\u7528\u4e8eslice\uff0cmap\uff0c\u548cchannel\u7684\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u533a\u522b\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>new \u7ed9\u4f60\u4e00\u4e2a\u6307\u9488,\u6307\u5411\u96f6\u503c,<\/strong><strong>\u9002\u7528\u4e8e int \u578b\u3001\u6570\u7ec4\u3001\u7ed3\u6784\u4f53\u7b49\u503c\u7c7b\u578b\u3002func new(Type) *Type<\/strong><\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>make\u53ea\u80fd\u7528\u4e8e slice\u3001map\u3001channel \u4e09\u79cd\u5185\u5efa\u5f15\u7528\u7c7b\u578b\u3002\u8fd4\u56de\u7c7b\u578b\u672c\u8eab\uff08\u975e\u6307\u9488)\u3002<\/strong><strong>func make(t Type, size &#8230;IntegerType) Type<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-medium-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u4e00\u3001new<\/mark><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">1.\u57fa\u672c\u7c7b\u578b(\u503c\u7c7b\u578b)<\/mark><\/strong><\/h3>\n\n\n\n<p>\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u58f0\u660e\u540e\u4e3anil,\u4e14\u4e0d\u53ef\u8d4b\u503c\uff0c\u5982\u4e0b\u793a\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-a2d9da44271da99d985e44061889b3fb\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    var test *int\n    fmt.Println(test)\n}\n<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u7a0b\u5e8f\u8fd4\u56denil\u503c,nil\u7684\u503c\u662f\u4e0d\u80fd\u76f4\u63a5\u8d4b\u503c\u7684,\u5982\u679c\u7ed9test\u8d4b\u503c\uff0c\u7a0b\u5e8f\u62a5\u9519<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-342013dcd01357002fa533e608826223\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    var test *int\n    fmt.Println(test)\n    *test = 8\n    fmt.Println(*test)\n}<\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u62a5\u9519:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-black-background-color has-text-color has-background has-link-color wp-elements-5dd951dd58ac9e60d82e6da1f2e91b8b\"><code>&lt;nil&gt;\npanic: runtime error: invalid memory address or nil pointer dereference\n&#91;signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x104abd7a0]\n\ngoroutine 1 &#91;running]:\nmain.main()\n        \/Users\/peter\/go\/src\/go-learning\/new\/main.go:10 +0x50\nexit status 2<\/code><\/pre>\n\n\n\n<p>\u5982\u4f55\u89e3\u51b3\uff1f\u901a\u8fc7Go\u63d0\u4f9b\u4e86new\u6765\u521d\u59cb\u5316\u4e00\u5730\u5740\u5c31\u53ef\u4ee5\u89e3\u51b3\u3002\u53ef\u4ee5\u7406\u89e3\u4e3a\u6307\u9488\u53d8\u91cf\u58f0\u660e\u540e\u672a\u5206\u914d\u5185\u5b58\u7a7a\u95f4,\u672a\u6307\u5411\u5185\u5b58\u5730\u5740\u5373nil,\u8fdb\u884c\u8d4b\u503c\u81ea\u7136\u5c31\u62a5\u9519\u3002\u901a\u8fc7new\u5206\u914d\u81f3\u5730\u5740\u540e\u6307\u9488\u53d8\u91cf\u6307\u5411\u5185\u5b58\u5730\u5740\uff0c\u5e76\u4e14\u521d\u59cb\u5316zero\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-2fa449fb270aad210800ad291e5592fc\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    var test *int\n    fmt.Println(test)\n    test = new(int)\n    fmt.Printf(\"\u7c7b\u578b:%T\\n\", test)\n    fmt.Println(*test)\n    *test = 8\n    fmt.Println(test)\n}<\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u8fd4\u56de:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-purple-color has-black-background-color has-text-color has-background has-link-color wp-elements-2c3703c72c454e190460c9d053707348\"><code>&lt;nil&gt;\n\u7c7b\u578b:*int\n0\n0x1400000e0d8<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7new\u51fd\u6570,\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u65b0\u5206\u914d\u7684\u7c7b\u578b\u4e3aint\u7684\u6307\u9488\uff0c\u6307\u9488\u503c\u4e3a0x1400000e0d8\uff0c\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u7684\u5185\u5bb9\u7684\u503c\u4e3a\u96f6\uff08zero value)\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-6bf3ef98927df5d726c70d9b4c4dd0b7\">\u540c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0d\u540c\u7684\u6307\u9488\u7c7b\u578b\u96f6\u503c\u662f\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-cyan-blue-color has-text-color has-link-color has-medium-font-size wp-elements-65d211ddbd2ee39a096302e585d93c5f\"><strong>2.\u6570\u7ec4(\u503c\u7c7b\u578b)<\/strong><\/h3>\n\n\n\n<p>\u6570\u7ec4\u7c7b\u578b\u5176\u5b9e\u8ddf\u57fa\u672c\u7c7b\u578b\u4e00\u6837,\u4e5f\u662f\u503c\u7c7b\u578b,\u901a\u8fc7new\u521b\u5efa\u4e0e\u57fa\u672c\u7c7b\u578b\u4e00\u6837,\u53ea\u4e0d\u8fc7\u53d8\u6210\u6570\u7ec4\u6307\u9488,\u5e76\u4e14\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u5730\u5740\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-62fdbe77d369bbad79ccf7cc356a3384\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/\u666e\u901a\u53d8\u91cf\n    var a int\n    fmt.Printf(\"\u666e\u901a\u53d8\u91cfa: %p %#v \\n\", &amp;a, a)\n    var b &#91;5]int\n    fmt.Printf(\"\u666e\u901a\u6570\u7ec4b: %p %#v \\n\", &amp;b, b)\n    b&#91;1] = 100\n    fmt.Printf(\"\u666e\u901a\u6570\u7ec4b\u8d4b\u503c: %p %#v \\n\", &amp;b, b)\n    c := new(&#91;5]int)\n    fmt.Printf(\"\u6570\u7ec4\u6307\u9488c: %p %#v \\n\", &amp;c, c)\n    (*c)&#91;0] = 8\n    fmt.Printf(\"\u6570\u7ec4\u6307\u9488c\u8d4b\u503c: %p %#v \\n\", &amp;c, c)\n    fmt.Printf(\"\u6570\u7ec4\u6307\u9488c\u9996\u5143\u7d20\u5730\u5740: %p %#v \\n\", &amp;c, (*c)&#91;0])\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-purple-color has-black-background-color has-text-color has-background has-link-color wp-elements-d0ea5772f243412cfd31ae3517bad130\"><code>\u666e\u901a\u53d8\u91cfa: 0x1400000e0d0 0 \n\u666e\u901a\u6570\u7ec4b: 0x1400001a240 &#91;5]int{0, 0, 0, 0, 0} \n\u666e\u901a\u6570\u7ec4b\u8d4b\u503c: 0x1400001a240 &#91;5]int{0, 100, 0, 0, 0} \n\u6570\u7ec4\u6307\u9488c: 0x14000056038 &amp;&#91;5]int{0, 0, 0, 0, 0} \n\u6570\u7ec4\u6307\u9488c\u8d4b\u503c: 0x14000056038 &amp;&#91;5]int{8, 0, 0, 0, 0} \n\u6570\u7ec4\u6307\u9488c\u9996\u5143\u7d20\u5730\u5740: 0x14000056038 8 <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-cyan-blue-color has-text-color has-link-color has-medium-font-size wp-elements-ff997dd9c7a0a1ed2c0a92f92d6b964e\"><strong>3.\u5f15\u7528\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>go\u4f1a\u7ed9\u5f15\u7528\u7c7b\u578b\u521d\u59cb\u5316\u4e3anil\uff0cnil\u662f\u4e0d\u80fd\u76f4\u63a5\u8d4b\u503c\u7684\u3002\u5e76\u4e14\u4e0d\u80fd\u7528new\u5206\u914d\u5185\u5b58,\u793a\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-e5c585056b731f288520db1a6269282c\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var m map&#91;string]string\n    fmt.Printf(\"m: %p %#v \\n\", &amp;m, m)\n    mv := new(map&#91;string]string)\n    fmt.Printf(\"mv: %p %#v \\n\", &amp;mv, mv)\n    (*mv)&#91;\"a\"] = \"a\"\n    fmt.Printf(\"mv: %p %#v \\n\", &amp;mv, mv)\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u62a5\u9519,new\u5bf9map\u5e76\u672a\u8d77\u5230\u521d\u59cb\u5316\u5185\u5b58\u4f5c\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-purple-color has-black-background-color has-text-color has-background has-link-color wp-elements-6447d035081fd47ee8986217c4c93f45\"><code>m: 0x140000a2028 map&#91;string]string(nil) \nmv: 0x140000a2038 &amp;map&#91;string]string(nil) \npanic: assignment to entry in nil map\n\ngoroutine 1 &#91;running]:\nmain.main()\n        \/Users\/peter\/go\/src\/go-learning\/new\/main.go:10 +0x148\nexit status 2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color has-link-color has-medium-font-size wp-elements-650eba1246a5a133841948cca7e35fee\"><strong>\u4e8c\u3001make<\/strong><\/h2>\n\n\n\n<p>\u90a3\u4e48\u7528make\u51fd\u6570\u5904\u7406\u4f1a\u662f\u600e\u4e48\u6837\u5462\uff1f\u793a\u4f8b:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-black-background-color has-text-color has-background has-link-color wp-elements-9ec492204f4586ecf215e9f78586fda3\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ \u5207\u7247\n    s := make(&#91;]int, 5)\n    fmt.Printf(\"\u5207\u7247s: %p %#v %T \\n\", &amp;s, s, s)\n    s&#91;0] = 1\n    fmt.Printf(\"\u5207\u7247s: %p %#v \\n\", &amp;s, s)\n    m := make(map&#91;string]string)\n    fmt.Printf(\"\u5b57\u5178m: %p %#v %T \\n\", &amp;m, m, m)\n    m&#91;\"m\"] = \"m\"\n    fmt.Printf(\"\u5b57\u5178m: %p %#v \\n\", &amp;m, m)\n    ch := make(chan string)\n    fmt.Printf(\"\u7ba1\u9053ch: %p %#v %T \\n\", &amp;ch, ch, ch)\n    go func(message string) {\n       ch &lt;- message \/\/ \u5b58\u6d88\u606f\n    }(\"Ping!\")\n    fmt.Println(&lt;-ch) \/\/ \u53d6\u6d88\u606f \/\/\"Ping!\"\n    close(ch)\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-purple-color has-black-background-color has-text-color has-background has-link-color wp-elements-c569793fded5f6405997ac14bc0a34fc\"><code>\u5207\u7247s: 0x1400000c018 &#91;]int{0, 0, 0, 0, 0} &#91;]int \n\u5207\u7247s: 0x1400000c018 &#91;]int{1, 0, 0, 0, 0} \n\u5b57\u5178m: 0x14000056038 map&#91;string]string{} map&#91;string]string \n\u5b57\u5178m: 0x14000056038 map&#91;string]string{\"m\":\"m\"} \n\u7ba1\u9053ch: 0x14000056040 (chan string)(0x14000074070) chan string \nPing!<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color has-link-color has-medium-font-size wp-elements-b6e0204cfe224c86013627e5525c6289\"><strong>\u4e09\u3001\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>new(T)<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u4e3a\u7c7b\u578b T \u5206\u914d\u4e00\u5757 <strong>\u96f6\u503c\u5185\u5b58<\/strong>\uff0c\u8fd4\u56de *T\uff08\u6307\u9488\uff09\u3002<\/li>\n\n\n\n<li>\u5e38\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\u6216\u7ed3\u6784\u4f53\u3002<\/li>\n\n\n\n<li>\u53ea\u662f\u7b80\u5355\u7684\u5185\u5b58\u5206\u914d\uff0c\u6ca1\u6709\u521d\u59cb\u5316\u5185\u90e8\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>make(T, args)<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u53ea\u80fd\u7528\u4e8e <strong>slice\u3001map\u3001chan<\/strong> \u8fd9\u4e09\u79cd\u5f15\u7528\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u9664\u4e86\u5206\u914d\u5185\u5b58\uff0c\u8fd8\u4f1a <strong>\u521d\u59cb\u5316\u5185\u90e8\u6570\u636e\u7ed3\u6784<\/strong>\uff0c\u5e76\u8fd4\u56de T \u672c\u8eab\uff08\u4e0d\u662f\u6307\u9488\uff09\u3002<\/li>\n\n\n\n<li>\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u5e95\u5c42\u7ed3\u6784\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u989d\u5916\u7684\u5143\u6570\u636e\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u4e2dnew\u548cmake\u90fd\u662f\u7528\u6765\u5185\u5b58\u5206\u914d\u7684\u539f\u8bed\uff08allocation primitives\uff09\u3002\u7b80\u5355\u7684\u8bf4\uff0cnew\u53ea\u5206\u914d\u5185\u5b58\uff0cmake\u7528\u4e8eslice\uff0cmap\uff0c\u548cc&#8230;<\/p>\n","protected":false},"author":1,"featured_media":94,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[43],"class_list":["post-86","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang","tag-golang"],"_links":{"self":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":9,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/97"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media\/94"}],"wp:attachment":[{"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudnote.cc\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}