合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 匿名字段 #### 定义 ``` <pre class="calibre13">``` 只提供类型而不写字段名的方式,也就是匿名字段,也称为嵌入字段 ``` ``` ``` <pre class="calibre14">``` <span class="token2">package</span> main <span class="token2">import</span> <span class="token4">"fmt"</span> <span class="token6">// go支持只提供类型而不写字段名的方式,也就是匿名字段,也称为嵌入字段</span> <span class="token6">//人</span> type Person struct <span class="token1">{</span> name string sex string age int <span class="token1">}</span> type Student struct <span class="token1">{</span> Person <span class="token6">//只提供了类型</span> id int addr string <span class="token1">}</span> func <span class="token3">main</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">// 初始化</span> s1 <span class="token1">:</span><span class="token">=</span> Student<span class="token1">{</span>Person<span class="token1">{</span><span class="token4">"5lmh"</span><span class="token1">,</span> <span class="token4">"man"</span><span class="token1">,</span> <span class="token5">20</span><span class="token1">}</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">,</span> <span class="token4">"bj"</span><span class="token1">}</span> fmt<span class="token1">.</span><span class="token3">Println</span><span class="token1">(</span>s1<span class="token1">)</span> s2 <span class="token1">:</span><span class="token">=</span> Student<span class="token1">{</span>Person<span class="token1">:</span> Person<span class="token1">{</span><span class="token4">"5lmh"</span><span class="token1">,</span> <span class="token4">"man"</span><span class="token1">,</span> <span class="token5">20</span><span class="token1">}</span><span class="token1">}</span> fmt<span class="token1">.</span><span class="token3">Println</span><span class="token1">(</span>s2<span class="token1">)</span> s3 <span class="token1">:</span><span class="token">=</span> Student<span class="token1">{</span>Person<span class="token1">:</span> Person<span class="token1">{</span>name<span class="token1">:</span> <span class="token4">"5lmh"</span><span class="token1">}</span><span class="token1">}</span> fmt<span class="token1">.</span><span class="token3">Println</span><span class="token1">(</span>s3<span class="token1">)</span> <span class="token1">}</span> ``` ```