文章列表


<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,竞争状态(Race Condition)是指多个goroutine并发访问共享数据时,导致数据不一致或不可预期的行为。竞争状态通常发生在多个goroutine同时读写同一内存位置时,如果没有采取适当的同步措施,就会导致数据竞争。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">竞争状态可能会导致以下问题:</p><ol style="box-sizing: border-box; padding: 0px 0px 0px 32px; -webkit-font-smoothing: antialiased; list-style-position: outside; list-style-image: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 1em; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;" class=" list-paddingleft-2"><li><p>数据竞争:当多个goroutine同时修改共享数据的不同部分时,可能会导致数据不一致。例如,一个goroutine读取了一个变量的值,另一个goroutine同时修改了这个变量的值,最终结果取决于哪个goroutine先完成操作。</p></li><li><p>死锁:当多个goroutine相互等待对方释放资源时,会导致程序无法继续执行。例如,一个goroutine持有资源A并等待资源B,而另一个goroutine持有资源B并等待资源A,这就会导致两个goroutine都无法继续执行。</p></li></ol><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">为了避免竞争状态,可以使用Go语言提供的以下同步机制:</p><ol style="box-sizing: border-box; padding: 0px 0px 0px 32px; -webkit-font-smoothing: antialiased; list-style-position: outside; list-style-image: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 1em; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;" class=" list-paddingleft-2"><li><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin-top: 0px; margin-bottom: 0px; line-height: 1.75;">Mutex(互斥锁):用于保护共享资源的访问,在同一时间只允许一个goroutine访问共享资源。示例代码如下:</p></li></ol><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> mutex sync.Mutex</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 保护共享资源的访问 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">processData</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">mutex.Lock()</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">defer</span> mutex.Unlock()</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 访问共享资源的代码 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin-top: 0px; margin-bottom: 0px; line-height: 1.75;">2.Channel(通道):用于在goroutine之间传递数据,可以确保数据传递的顺序和同步。示例代码如下:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 使用带缓冲通道进行同步 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> dataChan <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">chan</span> <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 生产者goroutine &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">go</span> <span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> i := <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">0</span>; i &lt; <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">100</span>; i++ {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">dataChan &lt;- i <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 发送数据到通道 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-built_in" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">close</span>(dataChan) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 关闭通道 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}()</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 消费者goroutine &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> data := <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">range</span> dataChan {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 处理接收到的数据 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin-top: 0px; margin-bottom: 0px; line-height: 1.75;">3.WaitGroup(等待组):用于等待一组goroutine执行完成后再继续执行。示例代码如下:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> wg sync.WaitGroup</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 启动多个goroutine执行任务 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> i := <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">0</span>; i &lt; <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">10</span>; i++ {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">wg.Add(<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">1</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 增加等待组的计数器 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">go</span> <span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">defer</span> wg.Done() <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 减少等待组的计数器 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 执行任务代码 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}()</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">wg.Wait() <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 等待所有goroutine执行完成后再继续执行后面的代码</span></td></tr></tbody></table></code></pre><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">Goroutine是Go语言中的轻量级线程,由Go运行时环境(Goroutine Scheduler)进行调度。相比于传统的线程,Goroutine更加轻量级,一个Go程序可以同时启动成千上万个Goroutine,而不会对系统产生太大的压力。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,使用关键字&quot;go&quot;来启动一个Goroutine。当一个函数调用被go关键字修饰时,该函数会在一个新的Goroutine中异步执行。Goroutine的启动和销毁都非常快速,因此可以在程序中频繁地使用go关键字来创建Goroutine。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了如何在Go中使用Goroutine:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> (</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;time&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">go</span> count(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;goroutine&quot;</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 在一个新的Goroutine中执行count函数 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;main goroutine&quot;</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 打印主Goroutine的输出 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">time.Sleep(time.Second) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 等待一段时间,以确保Goroutine有足够的时间执行完成 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">count</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(name <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">string</span>)</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> i := <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">0</span>; i &lt; <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">5</span>; i++ {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(name, <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;:&quot;</span>, i) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 打印该Goroutine的输出 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">time.Sleep(<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">50</span> * time.Millisecond) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 模拟一些耗时的操作 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们使用go关键字启动了一个新的Goroutine来执行count函数。在主Goroutine中,我们打印了一条消息(&quot;main goroutine&quot;),然后等待一段时间以确保Goroutine有足够的时间执行完成。在count函数中,我们使用一个循环打印了5次消息,并使用time.Sleep函数模拟了一些耗时的操作。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">运行上面的代码,你会看到&quot;main goroutine&quot;被首先打印出来,然后是&quot;goroutine : 0&quot;、&quot;goroutine : 1&quot;等消息交替输出。这表明主Goroutine和count函数在不同的Goroutine中并发执行。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">Go语言提供了强大的并发和并行支持,这使得开发者可以更轻松地编写高效的多线程应用程序。在Go中,并发和并行是通过Goroutines和Channels实现的。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;"><span style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; font-weight: 700;">并发(Concurrency)</span>:并发是指在一个程序中同时执行多个任务的能力。在Go中,当一个函数调用被go关键字修饰时,该函数会在一个新的Goroutine中异步执行。这样就可以在一个程序中同时执行多个函数,从而实现并发。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;"><span style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; font-weight: 700;">并行(Parallelism)</span>:并行是指同时执行多个操作的能力。在计算机科学中,并行通常是指在同一时刻执行多个操作的能力。在Go中,可以通过使用goroutine实现并行执行。goroutine是轻量级的执行线程,由Go运行时环境(Goroutine Scheduler)进行调度。一个Go程序可以同时启动成千上万个goroutine,而不会对系统产生太大的压力。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了如何在Go中使用goroutine实现并发和并行:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> (</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;time&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">go</span> count(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;goroutine&quot;</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 在一个新的Goroutine中执行count函数 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;main goroutine&quot;</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 打印主Goroutine的输出 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">time.Sleep(time.Second) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 等待一段时间,以确保Goroutine有足够的时间执行完成 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">count</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(name <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">string</span>)</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> i := <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">0</span>; i &lt; <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">5</span>; i++ {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(name, <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;:&quot;</span>, i) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 打印该Goroutine的输出 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">time.Sleep(<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">50</span> * time.Millisecond) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 模拟一些耗时的操作 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们使用go关键字启动了一个新的Goroutine来执行count函数。在主Goroutine中,我们打印了一条消息(&quot;main goroutine&quot;),然后等待一段时间以确保Goroutine有足够的时间执行完成。在count函数中,我们使用一个循环打印了5次消息,并使用time.Sleep函数模拟了一些耗时的操作。运行上面的代码,你会看到&quot;main goroutine&quot;被首先打印出来,然后是&quot;goroutine : 0&quot;、&quot;goroutine : 1&quot;等消息交替输出。这表明主Goroutine和count函数在不同的Goroutine中并发执行。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,标识符可以是公开的(public)或未公开的(private)。公开的标识符可以被其他包引用和使用,而未公开的标识符只能在其所在包内部使用。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,标识符的可见性是通过大小写字母来区分的。以大写字母开头的标识符是公开的,可以由其他包引用和使用;以小写字母开头的标识符是未公开的,只能在其所在包内部使用。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了公开和未公开的标识符:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 公开的标识符 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> PublicStruct <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">PublicField <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 未公开的标识符 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> privateStruct <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">PrivateField <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建公开的结构体实例,可以访问其字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">publicInstance := PublicStruct{PublicField: <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Hello&quot;</span>}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(publicInstance.PublicField) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出:Hello &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建未公开的结构体实例,无法访问其字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">privateInstance := privateStruct{PrivateField: <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;World&quot;</span>} <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 编译错误,不能访问privateStruct和其字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(privateInstance.PrivateField) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 编译错误,不能访问PrivateField字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PublicStruct</code>是一个公开的结构体类型,其字段<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PublicField</code>也是公开的。因此,在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中可以创建<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PublicStruct</code>的实例并访问其字段。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">而<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">privateStruct</code>是一个未公开的结构体类型,其字段<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PrivateField</code>也是未公开的。因此,在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中无法创建<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">privateStruct</code>的实例,也无法访问其字段。尝试这样做会导致编译错误。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,嵌入类型(Embedded Types)是指将一个类型作为另一个类型的成员进行嵌入。嵌入类型允许我们将一个类型的字段直接嵌入到另一个类型中,从而创建一种关联关系。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">嵌入类型可以是结构体、接口或其他类型。通过在类型前面使用<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">&amp;</code>符号来获取类型的地址,并将其作为嵌入类型的值。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了嵌入类型的用法:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> InnerStruct <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field1 <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field2 <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> OuterStruct <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">InnerStruct <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 嵌入类型 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field3 &nbsp; &nbsp; <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">outer := OuterStruct{</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">InnerStruct: InnerStruct{</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field1: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">42</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field2: <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Hello&quot;</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">},</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Field3: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">100</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(outer.Field1) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出:42 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(outer.Field2) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出:Hello &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(outer.Field3) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出:100 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个内部结构体<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">InnerStruct</code>,并使用它作为外部结构体<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">OuterStruct</code>的嵌入类型。通过这种方式,<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">OuterStruct</code>可以直接访问<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">InnerStruct</code>的字段。在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中,我们创建了一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">OuterStruct</code>的实例,并设置了其字段的值。通过<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">outer.Field1</code>、<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">outer.Field2</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">outer.Field3</code>,我们可以访问和操作这些字段。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">需要注意的是,嵌入类型不会创建一个新的实例。也就是说,如果外部结构体和嵌入类型有相同的字段名,那么外部结构体的字段会覆盖嵌入类型的字段。如果需要访问嵌入类型的字段,可以使用<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">.</code>操作符来明确指定访问的是哪个字段。例如:<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">outer.InnerStruct.Field1</code>。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,多态(Polymorphism)是指通过接口实现多个具体类型的行为。它是一种在运行时根据对象的实际类型来执行相应的方法的机制。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,接口是一种隐式的多态类型,它允许我们将实现该接口的任意类型的对象赋值给该接口类型的变量。通过接口,我们可以定义并使用具有相同行为的不同类型的对象。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了Go语言中的多态实现:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Shape <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">interface</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Circle <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">radius <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(c Circle)</span></span> Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.14</span> * c.radius * c.radius</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Rectangle <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">width, height <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(r Rectangle)</span></span> Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> r.width * r.height</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">shapes := []Shape{</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Circle{radius: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2.0</span>},</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Rectangle{width: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.0</span>, height: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">4.0</span>},</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Circle{radius: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">1.5</span>},</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">for</span> _, shape := <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">range</span> shapes {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Area:&quot;</span>, shape.Area())</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口,它包含了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area</code>的方法。然后,我们实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Rectangle</code>两个结构体,并为它们分别定义了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area</code>方法。这两个结构体都实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口,因此它们都可以被赋值给<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>类型的变量。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中,我们创建了一个包含不同形状的切片<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">shapes</code>,并将它们作为参数传递给<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area</code>方法。在循环中,我们通过调用每个形状的<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area</code>方法来计算它们的面积,并打印输出。由于我们使用了接口的多态性,程序可以处理任意实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口的对象。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,方法集(Method Set)是一种与特定类型关联的函数集合。方法集定义了哪些函数可以作为该类型的成员方法。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">方法集可以通过类型的方法接收者(Receiver)来确定。方法接收者定义了该方法与哪个类型相关联,并确定了该方法的访问权限。根据接收者的类型,可以将方法集分为内置方法集和自定义方法集。</p><ol style="box-sizing: border-box; padding: 0px 0px 0px 32px; -webkit-font-smoothing: antialiased; list-style-position: outside; list-style-image: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 1em; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;" class=" list-paddingleft-2"><li><p>内置方法集:Go语言内置了一些基本类型,如<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">int</code>、<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">string</code>、<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">bool</code>等,这些类型有一些预定义的方法。例如,<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">len()</code>函数可以用于获取字符串或切片的长度,<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">make()</code>函数可以用于创建切片或映射等。这些方法通常用于操作基本类型或进行常见的操作。</p></li><li><p>自定义方法集:对于自定义类型,可以定义与该类型相关的方法。这些方法可以在类型上执行特定的操作,提供对该类型的额外功能。</p></li></ol><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的示例代码,展示了方法集的用法:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义一个自定义类型 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Circle <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">radius <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义自定义方法集中的方法 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(c Circle)</span></span> Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.14</span> * c.radius * c.radius</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义另一个自定义方法集中的方法 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(c Circle)</span></span> Perimeter() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2</span> * <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.14</span> * c.radius</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建一个Circle对象 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">circle := Circle{radius: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2.0</span>}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 调用自定义方法集中的方法 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Area:&quot;</span>, circle.Area())</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Perimeter:&quot;</span>, circle.Perimeter())</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>的结构体,并定义了两个与该类型相关联的方法<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area()</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Perimeter()</code>。这两个方法都使用了接收者<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">(c Circle)</code>来指定它们与<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型相关联。在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中,我们创建了一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>对象,并调用了这两个方法来计算圆的面积和周长。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,接口(Interface)是一种类型,它定义了一组方法的签名,而不需要实现这些方法。接口提供了一种抽象的描述,用于定义对象的行为。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">Go语言中的接口是隐式实现的,即如果一个类型拥有接口中定义的所有方法,那么该类型就被认为实现了该接口。这种特性使得Go语言的接口非常灵活,可以在不同的类型之间实现相同的接口,从而实现多态行为。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">接口由方法和类型的定义组成。方法的定义包括方法名称、参数列表和返回值类型。类型的定义包括类型的名称和方法的列表。以下是一个接口的示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Shape <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">interface</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">Perimeter() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>的接口,它包含了两个方法<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area()</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Perimeter()</code>,分别用于计算形状的面积和周长。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">现在,我们可以定义一个实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口的结构体类型。以下是一个示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Circle <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">radius <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(c Circle)</span></span> Area() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.14</span> * c.radius * c.radius</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(c Circle)</span></span> Perimeter() <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">float64</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">return</span> <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2</span> * <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3.14</span> * c.radius</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>的结构体类型,它包含一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">radius</code>字段,并实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口中的两个方法<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Area()</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Perimeter()</code>。通过在结构体类型上定义方法,我们使得<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型成为了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口的实现者。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">现在,我们可以使用接口变量来引用实现了该接口的任意类型。以下是一个示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">PrintShapeInfo</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">(s Shape)</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Area:&quot;</span>, s.Area())</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Perimeter:&quot;</span>, s.Perimeter())</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PrintShapeInfo</code>的函数,它接受一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口类型的参数,并打印出该形状的面积和周长。由于<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口,我们可以将<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型的变量传递给<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PrintShapeInfo</code>函数。以下是一个示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">circle := Circle{radius: <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2.0</span>}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">PrintShapeInfo(circle) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 传递 Circle 类型的变量给 PrintShapeInfo 函数 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们创建了一个<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型的变量<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">circle</code>,并将其传递给<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">PrintShapeInfo</code>函数。由于<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Circle</code>类型实现了<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口,因此可以将其赋值给<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Shape</code>接口类型的变量。这样,我们就可以使用接口来操作不同类型的对象,而不需要关心它们的具体实现细节。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,结构体(Struct)是一种用户自定义的数据类型,它允许你组合不同类型的字段来表示一个复杂的数据结构。结构体可以被看作是一种模板,用于创建具有相同属性和方法的对象。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">下面是一个简单的结构体的示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义一个结构体类型 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Person <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">name <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">age &nbsp;<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建一个结构体变量 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">p1 := Person{<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Alice&quot;</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">25</span>}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 访问结构体字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Name:&quot;</span>, p1.name)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Age:&quot;</span>, p1.age)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Person</code>的结构体类型,它有两个字段<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">name</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">age</code>,分别表示人的姓名和年龄。然后,在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中,我们创建了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">p1</code>的<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Person</code>类型的变量,并为其字段赋值。最后,我们通过点操作符<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">.</code>来访问结构体变量的字段。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">除了基本类型字段之外,结构体还可以包含其他自定义结构体类型的字段,形成更复杂的数据结构。下面是一个嵌套结构体的示例代码:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义一个嵌套结构体类型 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Address <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">city &nbsp;<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">state <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 定义一个包含嵌套结构体类型的结构体类型 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">type</span> Person <span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">struct</span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">name <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">string</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">age &nbsp;<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">addr Address</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建一个结构体变量 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">p1 := Person{</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">name: <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;Alice&quot;</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">age: &nbsp;<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">30</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">addr: Address{</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">city: &nbsp;<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;New York&quot;</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">state: <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;NY&quot;</span>,</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">},</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 访问嵌套结构体字段 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;City:&quot;</span>, p1.addr.city)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;State:&quot;</span>, p1.addr.state)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在上面的代码中,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Address</code>的嵌套结构体类型,它有两个字段<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">city</code>和<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">state</code>,分别表示城市的名称和州/省的名称。然后,我们定义了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Person</code>的结构体类型,它包含一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">addr</code>的嵌套结构体类型的字段。在<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">main</code>函数中,我们创建了一个名为<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">p1</code>的<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">Person</code>类型的变量,并为其字段赋值。最后,我们通过链式点操作符<code class=" inline" style="box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px 2px; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;">.</code>来访问嵌套结构体变量的字段。</p><p><br/></p>

<p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 0px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">在Go语言中,引用类型(Reference Types)指的是指针类型、切片(Slice)、映射(Map)、通道(Channel)和函数类型。这些类型都是引用类型的示例,它们与基本类型(如整数、浮点数和布尔值)的主要区别在于它们是通过引用来存储值的,而不是直接存储值。</p><p>1.指针类型(Pointer):<br/>指针是一种特殊的数据类型,它存储了一个变量的内存地址。通过使用指针,可以在不直接访问变量的情况下,间接地访问和修改该变量的值。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">代码示例:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> num <span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> = <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">42</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> numPtr *<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> = &amp;num <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 创建指向 num 的指针 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;num:&quot;</span>, num)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;numPtr:&quot;</span>, *numPtr) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 访问指针指向的值 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">*numPtr = <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">99</span> <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 通过指针修改变量的值 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;num:&quot;</span>, num)</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">输出结果:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-makefile" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-section" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">num: 42 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-section" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">numPtr: 42 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-section" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">num: 99</span></td></tr></tbody></table></code></pre><p>2.切片(Slice):<br/>切片是对数组的抽象,它提供了一种动态长度的、可变长的序列类型。切片由底层的数组和三个属性组成:长度、容量和指向底层数组的指针。</p><p class="" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; margin-top: 14px; margin-bottom: 0px; font-size: 15px; line-height: 1.75; color: rgb(5, 7, 59); text-wrap: wrap;">代码示例:</p><pre style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin-top: 14px; margin-bottom: 0px; font-size: 15px; overflow: auto; border-radius: 6px; color: rgb(5, 7, 59);"><code class="language-go" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; margin: 0px; font-size: 1em; tab-size: 4;"><table class="hljs hljs-ln"><tbody style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;" class="firstRow"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">package</span> main</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">import</span> <span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;fmt&quot;</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-function" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">func</span> <span class="hljs-title" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(0, 127, 170); line-height: 24px;">main</span><span class="hljs-params" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">()</span></span> {</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> slice1 []<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> = []<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span>{<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">1</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">4</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">5</span>}</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><span class="hljs-keyword" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(121, 40, 161); line-height: 24px;">var</span> slice2 []<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span> = <span class="hljs-built_in" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">make</span>([]<span class="hljs-type" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">int</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">3</span>, <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">5</span>) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// len=3, cap=5 &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">slice2[<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">0</span>] = <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">10</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">slice2[<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">1</span>] = <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">20</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">slice2[<span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">2</span>] = <span class="hljs-number" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(170, 93, 0); line-height: 24px;">30</span> &nbsp;</td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><br/></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;slice1:&quot;</span>, slice1) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出: [1 2 3 4 5] &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">fmt.Println(<span class="hljs-string" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: green; line-height: 24px;">&quot;slice2:&quot;</span>, slice2) <span class="hljs-comment" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; color: rgb(105, 105, 105); line-height: 24px;">// 输出: [10 20 30] &nbsp;</span></td></tr><tr style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;"><td class="hljs-ln-line hljs-ln-numbers" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px; color: rgb(190, 191, 208); letter-spacing: 0px; max-width: 50px; overflow: hidden; text-align: justify; user-select: none; vertical-align: top; white-space-collapse: collapse;" width="30"><br/></td><td class="hljs-ln-line hljs-ln-code" style="box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; line-height: 24px;">}</td></tr></tbody></table></code></pre><p><br/></p>