25、Go 复合数据类型 - 数组之 new 函数
hi,我是温新
在 GO 语言中,new
和 make
都用来分配内存
。nwe
只用于内存分配,make
用于 slice
、map
、channel
的初始化。
new 函数
new(类型)
函数一个内置函数,用于分配内存。下面通过案例来学习 new 函数的使用。
已存在变量
对于一个已经存在的变量,可以使用指针进行赋值。
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">package</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">import</span> <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">"fmt"</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">func</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span>() {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 声明一个变量</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">var</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">int</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 把变量 p 的指针赋值给 变量 v</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">v</span> :<span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">&</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 通过指针修改值</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">v</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">fmt</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">.</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">Println</span>(<span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span>, <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">v</span>) <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 100 100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>
声明指针变量
1)声明指针变量
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">package</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">import</span> <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">"fmt"</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">func</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span>() {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 声明指针变量 p</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">var</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">int</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">fmt</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">.</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">Println</span>(<span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span>) <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 值为 <nil></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 对指针变量赋值</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>
1、声明了一个指针变量 p,此时在内存中开辟出一块空间,其存储的值为 <nil>
。注意开辟了内存空间,存的值是 nil
,但是没有分配内存地址;
2、*p
通过地址赋值,此时就会报错,原因是指针变量 p,开辟了内存空间但是没有内存地址,其报错如下:
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">panic</span>: <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">runtime</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">error</span>: <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">invalid</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">memory</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">address</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">or</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">nil</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">pointer</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">dereference</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">[<span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">signal</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0xc0000005</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">code</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0x1</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">addr</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0x0</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">pc</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">0xb9def1</span>]</span>
2)使用 new 函数分配内存地址
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">package</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">import</span> <span style="box-sizing: border-box;color: rgb(152, 195, 121) !important">"fmt"</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">func</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">main</span>() {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">var</span> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">int</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 使用 new 函数变指针变量初始化内存地址</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">new</span>(<span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">int</span>)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">fmt</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">.</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">Println</span>(<span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">*</span><span style="box-sizing: border-box;color: rgb(18, 170, 228) !important">p</span>)</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>
1、初始化一个指针变量时,它的值是 nil
,nil
无法直接赋值;
2、使用 new
函数返回一个指向新分配的类型为 int ·的指针,该指针指向的内容的值为 零,并值地址指向到指针变量 p;
3、为指针变量赋值。
请登录后再评论