合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 实例化结构体 # 实例化结构体的几种方式 ## ## 方式一: 定义结构体: ``` <pre class="calibre13">``` <span class="token6">/*匿名结构体*/</span> type Person struct <span class="token1">{</span> Name string Age int <span class="token1">}</span> ``` ``` 实例化: ``` <pre class="calibre13">``` <span class="token2">var</span> p Person <span class="token6">// 结构体实例化时 才会真正的分配内存</span> p<span class="token1">.</span>Name <span class="token">=</span> <span class="token4">"小红"</span> ``` ``` > 只有结构体实例化时 才能真正分配内存 ### 方式二: ``` <pre class="calibre13">``` <span class="token6">/*通过new关键字对结构体实例化 得到结构体地址*/</span> <span class="token2">var</span> p2 <span class="token">=</span> <span class="token2">new</span><span class="token1">(</span>Person<span class="token1">)</span> p2<span class="token1">.</span>Name <span class="token">=</span> <span class="token4">"p2 name"</span> p2<span class="token1">.</span>Age <span class="token">=</span> <span class="token5">20</span> fmt<span class="token1">.</span><span class="token3">Printf</span><span class="token1">(</span><span class="token4">"new实例p2类型 %T\n"</span><span class="token1">,</span> p2<span class="token1">)</span> <span class="token6">// new实例p2类型 *structs.Person</span> fmt<span class="token1">.</span><span class="token3">Printf</span><span class="token1">(</span><span class="token4">"new实例 p2=%#v\n"</span><span class="token1">,</span> p2<span class="token1">)</span> <span class="token6">// new实例 p2=&structs.Person{Name:"p2 name", Age:20}</span> ``` ``` ### 方式三: ``` <pre class="calibre13">``` <span class="token6">/*使用&对结构体进行取地址操作 相当于对结构体类型 进行了一次new实例化操作*/</span> p3 <span class="token1">:</span><span class="token">=</span> <span class="token">&</span>Person<span class="token1">{</span><span class="token1">}</span> p3<span class="token1">.</span>Name <span class="token">=</span> <span class="token4">"p3"</span> fmt<span class="token1">.</span><span class="token3">Printf</span><span class="token1">(</span><span class="token4">"%v"</span><span class="token1">,</span> p3<span class="token1">)</span> ``` ``` ## 批量实例化: make 切片 ``` <pre class="calibre14">``` <span class="token6">/*make实例化*/</span> m <span class="token1">:</span><span class="token">=</span> <span class="token3">make</span><span class="token1">(</span>map<span class="token1">[</span>string<span class="token1">]</span><span class="token">*</span>Person<span class="token1">)</span> pers <span class="token1">:</span><span class="token">=</span> <span class="token1">[</span><span class="token1">]</span>Person<span class="token1">{</span> <span class="token1">{</span>Name<span class="token1">:</span> <span class="token4">"make1"</span><span class="token1">,</span> Age<span class="token1">:</span> <span class="token5">19</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>Name<span class="token1">:</span> <span class="token4">"make2"</span><span class="token1">,</span> Age<span class="token1">:</span> <span class="token5">19</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">}</span> <span class="token2">for</span> _<span class="token1">,</span> per <span class="token1">:</span><span class="token">=</span> range pers <span class="token1">{</span> m<span class="token1">[</span>per<span class="token1">.</span>Name<span class="token1">]</span> <span class="token">=</span> <span class="token">&</span>per <span class="token1">}</span> <span class="token2">for</span> k<span class="token1">,</span> v <span class="token1">:</span><span class="token">=</span> range m <span class="token1">{</span> fmt<span class="token1">.</span><span class="token3">Println</span><span class="token1">(</span>k<span class="token1">,</span> <span class="token4">"--->"</span><span class="token1">,</span> v<span class="token1">.</span>Name<span class="token1">)</span> <span class="token1">}</span> ``` ```