教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go语言基础学习

go语言基础学习

发布时间:2022-03-20   编辑:jiaochengji.com
教程集为您提供go语言基础学习等资源,欢迎您收藏本站,我们将为您提供最新的go语言基础学习资源
<h2 id="articleHeader0" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.75em; border-bottom:1px solid rgb(238,238,238); padding-bottom:10px"> 一、函数function</h2> <h3 id="articleHeader1" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 1、基本概念</h3>

函数是基本的代码块,用于执行一个任务。
<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">Go 语言最少有个 main() 函数</code>。
<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">函数声明告诉了编译器函数的名称,返回类型,和参数</code>。
Go 语言标准库提供了多种可动用的内置的函数。例如,len() 函数可以接受不同类型参数并返回该类型的长度。如果我们传入的是字符串则返回字符串的长度,如果传入的是数组,则返回数组中包含的函数个数。

<h3 id="articleHeader2" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 2、函数定义</h3>

函数定义格式如下:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">function_name</span><span class="hljs-params" style="">( [parameter list] )</span> [<span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">return_types</span>]</span> { 函数体 }</code></pre>

函数定义解析:

<ul style="margin:1.5em 0px 1.5em 3em; padding-left:0px; color:rgb(51,51,51); font-size:14px"><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">func</code>:函数由 func 开始声明</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">function_name</code>:函数名称,函数名和参数列表一起构成了函数签名。</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">parameter list</code>:参数列表,<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">参数就像一个占位符</code>,当函数被调用时,你可以将值传递给参数,这个值被称为实际参数。参数列表指定的是<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">参数类型、顺序、及参数个数</code>。参数是<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">可选</code>的,也就是说函数也可以不包含参数。</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">return_types</code>:返回类型,函数返回一列值。return_types 是该列值的数据类型。有些功能不需要返回值,这种情况下 return_types 不是必须的。</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">函数体</code>:函数定义的代码集合。</li></ul>

示例:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-comment" style="color:rgb(153,153,136)">/* 函数返回两个数的最大值 */</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">max</span><span class="hljs-params" style="">(num1, num2 <span class="hljs-keyword" style="font-weight:bold">int</span>)</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">int</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">/* 声明局部变量 */</span> <span class="hljs-keyword" style="font-weight:bold">var</span> result <span class="hljs-keyword" style="font-weight:bold">int</span> <span class="hljs-keyword" style="font-weight:bold">if</span> (num1 > num2) { result = num1 } <span class="hljs-keyword" style="font-weight:bold">else</span> { result = num2 } <span class="hljs-keyword" style="font-weight:bold">return</span> result }</code></pre> <h3 id="articleHeader3" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 3、函数特性</h3> <ul style="margin:1.5em 0px 1.5em 3em; padding-left:0px; color:rgb(51,51,51); font-size:14px"><li style="margin:0.3em 0px">Go 函数<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">不支持</code><span style="">嵌套、重载和默认参数</span></li><li style="margin:0.3em 0px">Go 函数<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">支持</code>无需声明原型、不定长度变参、多返回值、命名返回值参数、匿名函数、闭包</li><li style="margin:0.3em 0px">定义函数使用关键字<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">func</code>,且左大括号不能另起一行</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">函数也可以作为一种类型使用</code></li></ul><pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">ackage main <span class="hljs-keyword" style="font-weight:bold">import</span> <span class="hljs-string" style="color:rgb(221,17,68)">"fmt"</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { A(<span class="hljs-number" style="color:rgb(0,128,128)">1</span>, <span class="hljs-number" style="color:rgb(0,128,128)">2</span>, <span class="hljs-number" style="color:rgb(0,128,128)">3</span>, <span class="hljs-number" style="color:rgb(0,128,128)">4</span> ,<span class="hljs-number" style="color:rgb(0,128,128)">5</span>) } <span class="hljs-comment" style="color:rgb(153,153,136)">// ... 不定长变参</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">A</span><span class="hljs-params" style="">(a ...<span class="hljs-keyword" style="font-weight:bold">int</span>)</span></span> { fmt.Println(a) }</code></pre>

输出:

<pre class="hljs lsl" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run func.go [<span class="hljs-number" style="color:rgb(0,128,128)">1</span> <span class="hljs-number" style="color:rgb(0,128,128)">2</span> <span class="hljs-number" style="color:rgb(0,128,128)">3</span> <span class="hljs-number" style="color:rgb(0,128,128)">4</span> <span class="hljs-number" style="color:rgb(0,128,128)">5</span>]</code></pre>

<span style="">不定长变参特性:</span>
1、不可以在不定长变参后边添加其他参数 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">func b(a ...int, b string)</code>, 这种写法是<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">错误</code>的
2、不定长参数可以放在其他参数后边 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">func b(b string, a ...int)</code>

<h3 id="articleHeader4" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 4、匿名函数</h3> <pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:1.5em!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">// 将一个函数赋值一个变量,该变量是函数类型</span> a := <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span><span class="hljs-params" style="">()</span></span>{ fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"匿名函数"</span>) } <span class="hljs-comment" style="color:rgb(153,153,136)">// 调用</span> a() } </code></pre> <h3 id="articleHeader5" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 5、闭包</h3> <pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:1.5em!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-comment" style="color:rgb(153,153,136)">/** * 闭包函数 * * 该闭包函数接收一个int型参数,其返回值是函数类型 * */</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">closure</span><span class="hljs-params" style="">(x <span class="hljs-keyword" style="font-weight:bold">int</span>)</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">func</span><span class="hljs-params" style="">(<span class="hljs-keyword" style="font-weight:bold">int</span>)</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">int</span></span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"%p\n"</span>, &x) <span class="hljs-keyword" style="font-weight:bold">return</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-params" style="">(y <span class="hljs-keyword" style="font-weight:bold">int</span>)</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">int</span></span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"%p\n"</span>, &x) fmt.Println(x) fmt.Println(y) <span class="hljs-keyword" style="font-weight:bold">return</span> x y } } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { f := closure(<span class="hljs-number" style="color:rgb(0,128,128)">10</span>); fmt.Println(f(<span class="hljs-number" style="color:rgb(0,128,128)">1</span>)) fmt.Println(f(<span class="hljs-number" style="color:rgb(0,128,128)">2</span>)) } </code></pre>

打印结果:

<pre class="hljs lsl" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run func.go %p <span class="hljs-number" style="color:rgb(0,128,128)">0xc42000e228</span> %p <span class="hljs-number" style="color:rgb(0,128,128)">0xc42000e228</span> <span class="hljs-number" style="color:rgb(0,128,128)">10</span> <span class="hljs-number" style="color:rgb(0,128,128)">1</span> <span class="hljs-number" style="color:rgb(0,128,128)">11</span> %p <span class="hljs-number" style="color:rgb(0,128,128)">0xc42000e228</span> <span class="hljs-number" style="color:rgb(0,128,128)">10</span> <span class="hljs-number" style="color:rgb(0,128,128)">2</span> <span class="hljs-number" style="color:rgb(0,128,128)">12</span> ➜ myfirstgo</code></pre> <h3 id="articleHeader6" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 6、defer</h3> <ul style="margin:1.5em 0px 1.5em 3em; padding-left:0px; color:rgb(51,51,51); font-size:14px"><li style="margin:0.3em 0px">defer 的执行方式类似其他语言中的<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">析构函数</code>,在函数体执行结束后按照调用顺序的<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">相反顺序</code>逐个执行</li><li style="margin:0.3em 0px">即使函数发生<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">严重错误</code>也会执行</li><li style="margin:0.3em 0px">支持匿名函数的调用</li><li style="margin:0.3em 0px">常用于<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">资源清理、文件关闭、解锁以及记录时间</code>等操作</li><li style="margin:0.3em 0px">通过与匿名函数配合可在return之后<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">修改</code>函数计算结果</li><li style="margin:0.3em 0px">如果函数体内某个变量作为defer时匿名函数的参数,则在定义defer时即已经获得了拷贝,否则则是引用某个变量的地址</li><li style="margin:0.3em 0px">Go没有异常机制,但有 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">panic/recover</code> 模式来处理错误</li><li style="margin:0.3em 0px"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">Panic</code> 可以在任何地方引发,但 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">recover</code> <span style="">只有</span>在 <span style="">defer</span> 调用的函数中有效</li></ul>

简单的测试:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"a"</span>) <span class="hljs-keyword" style="font-weight:bold">defer</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"b"</span>) <span class="hljs-keyword" style="font-weight:bold">defer</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"c"</span>) }</code></pre>

上边的执行会打印什么结果呢? 会打印:a b c 吗?
让我们实际执行一下:

<pre class="hljs stylus" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"> myfirstgo go run func<span class="hljs-selector-class" style="">.go</span> <span class="hljs-selector-tag" style="font-weight:bold">a</span> c b</code></pre>

实际打印的结果为:a c b

<blockquote style="padding:10px 20px; margin:1.5em 0px; font-size:14px; border-left:2px solid rgb(0,154,97); background:rgb(246,246,246); color:rgb(85,85,85)">

defer 的执行方式类似其他语言中的<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">析构函数</code>,在<span style="">函数体执行结束后</span>按照调用顺序的<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">相反顺序</code>逐个执行

</blockquote>

<span style="">使用闭包</span>

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-keyword" style="font-weight:bold">for</span> i := <span class="hljs-number" style="color:rgb(0,128,128)">0</span>; i < <span class="hljs-number" style="color:rgb(0,128,128)">3</span>; i { <span class="hljs-comment" style="color:rgb(153,153,136)">// defer 普通调用</span> <span class="hljs-comment" style="color:rgb(153,153,136)">// defer fmt.Println(i) // 打印 2 1 0</span> <span class="hljs-comment" style="color:rgb(153,153,136)">// 使用闭包,引用局部变量</span> <span class="hljs-keyword" style="font-weight:bold">defer</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-params" style="">()</span></span> { fmt.Println(i) }() } }</code></pre>

打印结果:

<pre class="hljs autoit" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go <span class="hljs-built_in" style="color:rgb(0,134,179)">run</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span>.<span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">go</span></span> <span class="hljs-number" style="color:rgb(0,128,128)">3</span> <span class="hljs-number" style="color:rgb(0,128,128)">3</span> <span class="hljs-number" style="color:rgb(0,128,128)">3</span></code></pre>

<span style="">panic 使用示例</span>:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { A() B() C() } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">A</span><span class="hljs-params" style="">()</span></span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"FUNC A"</span>) } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">B</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">// 匿名函数,如果没有参数,则末尾需要使用括号</span> <span class="hljs-keyword" style="font-weight:bold">defer</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-keyword" style="font-weight:bold">if</span> err := <span class="hljs-built_in" style="color:rgb(0,134,179)">recover</span>(); err != <span class="hljs-literal" style="color:rgb(0,128,128)">nil</span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"Recover is B"</span>) } }() <span class="hljs-built_in" style="color:rgb(0,134,179)">panic</span>(<span class="hljs-string" style="color:rgb(221,17,68)">"B panic"</span>) } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">C</span><span class="hljs-params" style="">()</span></span> { fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"FUNC C"</span>) }</code></pre>

打印结果:

<pre class="hljs autoit" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go <span class="hljs-built_in" style="color:rgb(0,134,179)">run</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span>.<span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">go</span></span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">FUNC</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">A</span></span> Recover is B <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">FUNC</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">C</span></span> ➜ myfirstgo</code></pre> <h2 id="articleHeader7" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.75em; border-bottom:1px solid rgb(238,238,238); padding-bottom:10px"> 二、结构Struct</h2> <h3 id="articleHeader8" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 1.基本概念</h3>

Go 语言中数组可以存储同一类型的数据,但<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">在结构体中我们可以为不同项定义不同的数据类型</code>。
<span style="">结构体</span>是<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">由一系列具有相同类型或不同类型的数据构成的数据集合</code>。
结构体表示一项记录,比如保存图书馆的书籍记录,每本书有以下属性:
Title :标题
Author : 作者
Subject:学科
ID:书籍ID

<h3 id="articleHeader9" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 2.结构体定义</h3>

结构体定义需要使用 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">type</code> 和 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">struct</code> 语句。struct 语句定义一个新的数据类型,结构体中有一个或多个成员。type 语句设定了结构体的名称。结构体的格式如下:

<pre class="hljs fsharp" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-class" style=""><span class="hljs-keyword" style="font-weight:bold">type</span> <span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">struct_variable_type</span> <span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">struct</span> {</span> <span class="hljs-keyword" style="font-weight:bold">member</span> definition; <span class="hljs-keyword" style="font-weight:bold">member</span> definition; ... <span class="hljs-keyword" style="font-weight:bold">member</span> definition; }</code></pre>

一旦定义了结构体类型,它就能用于变量的声明,语法格式如下:

<pre class="hljs ceylon" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:initial; word-break:break-word"><span class="hljs-keyword" style="font-weight:bold">variable</span><span class="hljs-number" style="color:rgb(0,128,128)">_n</span>ame := structure<span class="hljs-number" style="color:rgb(0,128,128)">_</span><span class="hljs-keyword" style="font-weight:bold">variable</span><span class="hljs-number" style="color:rgb(0,128,128)">_</span>type {<span class="hljs-keyword" style="font-weight:bold">value</span><span class="hljs-number" style="color:rgb(0,128,128)">1</span>, <span class="hljs-keyword" style="font-weight:bold">value</span><span class="hljs-number" style="color:rgb(0,128,128)">2</span>...valuen}</code></pre>

type 是定义名称,struct 是结构体类型,如同 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">int</code> 类型一样。

结构体可以包含多种数据类型,数组只能是单一类型的数据集合。

访问结构体成员
如果要访问结构体成员,需要使用点号 (.) 操作符,格式为:<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">"结构体.成员名"</code>。
结构体类型变量使用<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">struct</code>关键字定义,实例如下:

<pre class="hljs armasm" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-symbol" style="color:rgb(153,0,115)">package</span> main <span class="hljs-symbol" style="color:rgb(153,0,115)">import</span> <span class="hljs-string" style="color:rgb(221,17,68)">"fmt"</span> <span class="hljs-symbol" style="color:rgb(153,0,115)">type</span> <span class="hljs-keyword" style="font-weight:bold">Books </span><span class="hljs-keyword" style="font-weight:bold">struct </span>{ title <span class="hljs-keyword" style="font-weight:bold">string </span> author <span class="hljs-keyword" style="font-weight:bold">string </span> <span class="hljs-keyword" style="font-weight:bold">subject </span><span class="hljs-keyword" style="font-weight:bold">string </span> <span class="hljs-keyword" style="font-weight:bold">book_id </span>int } <span class="hljs-symbol" style="color:rgb(153,0,115)">func</span> main() { var <span class="hljs-keyword" style="font-weight:bold">Book1 </span><span class="hljs-keyword" style="font-weight:bold">Books </span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* 声明 Book1 为 Books 类型 */</span> var <span class="hljs-keyword" style="font-weight:bold">Book2 </span><span class="hljs-keyword" style="font-weight:bold">Books </span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* 声明 Book2 为 Books 类型 */</span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* book 1 描述 */</span> <span class="hljs-keyword" style="font-weight:bold">Book1.title </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"Go 语言"</span> <span class="hljs-keyword" style="font-weight:bold">Book1.author </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"www.runoob.com"</span> <span class="hljs-keyword" style="font-weight:bold">Book1.subject </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"Go 语言教程"</span> <span class="hljs-keyword" style="font-weight:bold">Book1.book_id </span>= <span class="hljs-number" style="color:rgb(0,128,128)">6495407</span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* book 2 描述 */</span> <span class="hljs-keyword" style="font-weight:bold">Book2.title </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"Python 教程"</span> <span class="hljs-keyword" style="font-weight:bold">Book2.author </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"www.runoob.com"</span> <span class="hljs-keyword" style="font-weight:bold">Book2.subject </span>= <span class="hljs-string" style="color:rgb(221,17,68)">"Python 语言教程"</span> <span class="hljs-keyword" style="font-weight:bold">Book2.book_id </span>= <span class="hljs-number" style="color:rgb(0,128,128)">6495700</span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* 打印 Book1 信息 */</span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 1 title : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book1.title) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 1 author : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book1.author) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 1 subject : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book1.subject) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 1 book_id : %d\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book1.book_id) </span> <span class="hljs-comment" style="color:rgb(153,153,136)">/* 打印 Book2 信息 */</span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 2 title : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book2.title) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 2 author : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book2.author) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 2 subject : %s\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book2.subject) </span> fmt.Printf( <span class="hljs-string" style="color:rgb(221,17,68)">"Book 2 book_id : %d\n"</span>, <span class="hljs-keyword" style="font-weight:bold">Book2.book_id) </span>}</code></pre>

以上实例执行运行结果为:

<pre class="hljs armasm" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">1</span> title : Go 语言 <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">1</span> author : www.runoob.com <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">1</span> <span class="hljs-keyword" style="font-weight:bold">subject </span>: Go 语言教程 <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">1</span> <span class="hljs-keyword" style="font-weight:bold">book_id </span>: <span class="hljs-number" style="color:rgb(0,128,128)">6495407</span> <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">2</span> title : Python 教程 <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">2</span> author : www.runoob.com <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">2</span> <span class="hljs-keyword" style="font-weight:bold">subject </span>: Python 语言教程 <span class="hljs-keyword" style="font-weight:bold">Book </span><span class="hljs-number" style="color:rgb(0,128,128)">2</span> <span class="hljs-keyword" style="font-weight:bold">book_id </span>: <span class="hljs-number" style="color:rgb(0,128,128)">6495700</span></code></pre> <h3 id="articleHeader10" style="font-weight:500; line-height:1.2; color:rgb(51,51,51); margin:1.5em 0px 0px; font-size:1.5em"> 3.struct特性</h3> <ul style="margin:1.5em 0px 1.5em 3em; padding-left:0px; color:rgb(51,51,51); font-size:14px"><li style="margin:0.3em 0px">Go中的struct与C中的struct非常相似,并且Go没有class</li><li style="margin:0.3em 0px">使用 <code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">type <Name> struct{}</code> 定义结构,名称遵循可见性规则</li><li style="margin:0.3em 0px">支持指向自身的指针类型成员</li><li style="margin:0.3em 0px">支持<code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:2px 4px; color:rgb(199,37,78); background-color:rgb(249,242,244)">匿名</code>结构,可用作成员或定义成员变量</li><li style="margin:0.3em 0px">匿名结构也可以用于map的值</li><li style="margin:0.3em 0px">可以使用字面值对结构进行初始化</li><li style="margin:0.3em 0px">允许直接通过指针来读写结构成员</li><li style="margin:0.3em 0px">相同类似的成员可进行直接拷贝赋值</li><li style="margin:0.3em 0px">支持 == 与 != 比较运算符,但不支持 > 或 <</li><li style="margin:0.3em 0px">支持匿名字段,本质上是定义了以某个类型名为名称的字段</li><li style="margin:0.3em 0px">嵌入结构作为匿名字段看起来像继承,但不是继承</li><li style="margin:0.3em 0px">可以使用匿名字段指针</li></ul>

示例:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-keyword" style="font-weight:bold">package</span> main <span class="hljs-keyword" style="font-weight:bold">import</span> <span class="hljs-string" style="color:rgb(221,17,68)">"fmt"</span> <span class="hljs-keyword" style="font-weight:bold">type</span> person <span class="hljs-keyword" style="font-weight:bold">struct</span>{ Name <span class="hljs-keyword" style="font-weight:bold">string</span> Age <span class="hljs-keyword" style="font-weight:bold">int</span> } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { a := person{ Name:<span class="hljs-string" style="color:rgb(221,17,68)">"Jam"</span>, Age:<span class="hljs-number" style="color:rgb(0,128,128)">19</span>, } <span class="hljs-comment" style="color:rgb(153,153,136)">// a.Age = 20</span> fmt.Println(a) }</code></pre>

打印:

<pre class="hljs capnproto" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run <span class="hljs-class" style=""><span class="hljs-keyword" style="font-weight:bold">struct</span>.<span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">go</span> </span>{Jam <span class="hljs-number" style="color:rgb(0,128,128)">19</span>}</code></pre>

传递指针变量:

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-keyword" style="font-weight:bold">package</span> main <span class="hljs-keyword" style="font-weight:bold">import</span> <span class="hljs-string" style="color:rgb(221,17,68)">"fmt"</span> <span class="hljs-keyword" style="font-weight:bold">type</span> person <span class="hljs-keyword" style="font-weight:bold">struct</span>{ Name <span class="hljs-keyword" style="font-weight:bold">string</span> Age <span class="hljs-keyword" style="font-weight:bold">int</span> } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { a := person{ Name:<span class="hljs-string" style="color:rgb(221,17,68)">"Jam"</span>, Age:<span class="hljs-number" style="color:rgb(0,128,128)">19</span>, } <span class="hljs-comment" style="color:rgb(153,153,136)">// a.Age = 20</span> fmt.Println(a) A(a) <span class="hljs-comment" style="color:rgb(153,153,136)">// 值拷贝,如果需要原来的改变,则需要添加指针</span> B(&a) fmt.Println(a) } <span class="hljs-comment" style="color:rgb(153,153,136)">// per 为变量名,person表示为结构体类型</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">A</span><span class="hljs-params" style="">(per person)</span></span> { per.Age = <span class="hljs-number" style="color:rgb(0,128,128)">25</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"A"</span>, per) } <span class="hljs-comment" style="color:rgb(153,153,136)">// per 为变量名,person表示为结构体类型</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">B</span><span class="hljs-params" style="">(per *person)</span></span> { per.Age = <span class="hljs-number" style="color:rgb(0,128,128)">18</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"B"</span>, per) }</code></pre>

打印:

<pre class="hljs mipsasm" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run struct.go {<span class="hljs-keyword" style="font-weight:bold">Jam </span><span class="hljs-number" style="color:rgb(0,128,128)">19</span>} A {<span class="hljs-keyword" style="font-weight:bold">Jam </span><span class="hljs-number" style="color:rgb(0,128,128)">25</span>} <span class="hljs-keyword" style="font-weight:bold">B </span>&{<span class="hljs-keyword" style="font-weight:bold">Jam </span><span class="hljs-number" style="color:rgb(0,128,128)">18</span>} {<span class="hljs-keyword" style="font-weight:bold">Jam </span><span class="hljs-number" style="color:rgb(0,128,128)">18</span>}</code></pre>

或者在初始化结构体时,获取到变量地址并赋值变量,这样做的好处是在传递参数时,不需要传递地址符号了,只需在函数定义时,给参数加星号即可。

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-keyword" style="font-weight:bold">package</span> main <span class="hljs-keyword" style="font-weight:bold">import</span> <span class="hljs-string" style="color:rgb(221,17,68)">"fmt"</span> <span class="hljs-keyword" style="font-weight:bold">type</span> person <span class="hljs-keyword" style="font-weight:bold">struct</span>{ Name <span class="hljs-keyword" style="font-weight:bold">string</span> Age <span class="hljs-keyword" style="font-weight:bold">int</span> } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">// 在结构初识化时,我们习惯取地址符号,这样a就为指向某个结构的指针</span> a := &person{ Name:<span class="hljs-string" style="color:rgb(221,17,68)">"Jam"</span>, Age:<span class="hljs-number" style="color:rgb(0,128,128)">19</span>, } a.Name = <span class="hljs-string" style="color:rgb(221,17,68)">"Corwien"</span> <span class="hljs-comment" style="color:rgb(153,153,136)">// a.Age = 20</span> fmt.Println(a) A(a) <span class="hljs-comment" style="color:rgb(153,153,136)">// 值拷贝,如果需要原来的改变,则需要添加指针</span> B(a) fmt.Println(a) } <span class="hljs-comment" style="color:rgb(153,153,136)">// per 为变量名,person表示为结构体类型</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">A</span><span class="hljs-params" style="">(per *person)</span></span> { per.Age = <span class="hljs-number" style="color:rgb(0,128,128)">25</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"A"</span>, per) } <span class="hljs-comment" style="color:rgb(153,153,136)">// per 为变量名,person表示为结构体类型</span> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">B</span><span class="hljs-params" style="">(per *person)</span></span> { per.Age = <span class="hljs-number" style="color:rgb(0,128,128)">18</span> fmt.Println(<span class="hljs-string" style="color:rgb(221,17,68)">"B"</span>, per) } </code></pre>

打印:

<pre class="hljs dust" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="xml" style="">➜ myfirstgo go run struct.go &</span><span class="hljs-template-variable" style="color:rgb(0,128,128)">{Corwien 19}</span><span class="xml" style=""> A &</span><span class="hljs-template-variable" style="color:rgb(0,128,128)">{Corwien 25}</span><span class="xml" style=""> B &</span><span class="hljs-template-variable" style="color:rgb(0,128,128)">{Corwien 18}</span><span class="xml" style=""> &</span><span class="hljs-template-variable" style="color:rgb(0,128,128)">{Corwien 18}</span></code><span class="xml" style=""/></pre>

<span style="">匿名结构:</span>
匿名结构,没有名称的结构体

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">// 匿名结构,没有名称的结构体</span> a := <span class="hljs-keyword" style="font-weight:bold">struct</span> { Name <span class="hljs-keyword" style="font-weight:bold">string</span> Age <span class="hljs-keyword" style="font-weight:bold">int</span> }{ Name:<span class="hljs-string" style="color:rgb(221,17,68)">"Corwien"</span>, Age: <span class="hljs-number" style="color:rgb(0,128,128)">20</span>, } fmt.Println(a) }</code></pre>

打印:

<pre class="hljs capnproto" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run <span class="hljs-class" style=""><span class="hljs-keyword" style="font-weight:bold">struct</span>.<span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">go</span> </span>{Corwien <span class="hljs-number" style="color:rgb(0,128,128)">20</span>}</code></pre>

<span style="">匿名结构嵌套:</span>

<pre class="hljs stylus" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">type person struct{ Name string Age int Contact struct { Phone, City string Code int <span class="hljs-comment" style="color:rgb(153,153,136)">// 门牌号</span> } } func main() { <span class="hljs-selector-tag" style="font-weight:bold">a</span> := person{Name:<span class="hljs-string" style="color:rgb(221,17,68)">"Corwien"</span>, Age:<span class="hljs-number" style="color:rgb(0,128,128)">15</span>} <span class="hljs-selector-tag" style="font-weight:bold">a</span><span class="hljs-selector-class" style="">.Contact</span><span class="hljs-selector-class" style="">.Phone</span> = <span class="hljs-string" style="color:rgb(221,17,68)">"10086"</span> <span class="hljs-selector-tag" style="font-weight:bold">a</span><span class="hljs-selector-class" style="">.Contact</span><span class="hljs-selector-class" style="">.City</span> = <span class="hljs-string" style="color:rgb(221,17,68)">"Guangzhou"</span> <span class="hljs-selector-tag" style="font-weight:bold">a</span><span class="hljs-selector-class" style="">.Contact</span><span class="hljs-selector-class" style="">.Code</span> = <span class="hljs-number" style="color:rgb(0,128,128)">2007</span> fmt.Println(a) } </code></pre>

打印:

<pre class="hljs capnproto" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run <span class="hljs-class" style=""><span class="hljs-keyword" style="font-weight:bold">struct</span>.<span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">go</span> </span>{Corwien <span class="hljs-number" style="color:rgb(0,128,128)">15</span> {<span class="hljs-number" style="color:rgb(0,128,128)">10086</span> Guangzhou <span class="hljs-number" style="color:rgb(0,128,128)">2007</span>}}</code></pre>

<span style="">匿名字段:</span>

匿名字段:结构体没有命名结构体属性的字段,只有类型,匿名字段必须严格遵守字段类型声明的顺序。

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit"><span class="hljs-keyword" style="font-weight:bold">type</span> person <span class="hljs-keyword" style="font-weight:bold">struct</span>{ <span class="hljs-keyword" style="font-weight:bold">string</span> <span class="hljs-keyword" style="font-weight:bold">int</span> } <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:bold">main</span><span class="hljs-params" style="">()</span></span> { <span class="hljs-comment" style="color:rgb(153,153,136)">// 匿名字段必须严格遵守字段类型声明的顺序</span> a := person{<span class="hljs-string" style="color:rgb(221,17,68)">"Corwien"</span>, <span class="hljs-number" style="color:rgb(0,128,128)">12</span>} fmt.Println(a) }</code></pre>

打印:

<pre class="hljs capnproto" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">➜ myfirstgo go run <span class="hljs-class" style=""><span class="hljs-keyword" style="font-weight:bold">struct</span>.<span class="hljs-title" style="color:rgb(68,85,136); font-weight:bold">go</span> </span>{Corwien <span class="hljs-number" style="color:rgb(0,128,128)">12</span>}</code></pre>

<span style="">结构类型比较</span>

<pre class="hljs go" style="overflow:auto; font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:0.93em; padding:1em; margin-bottom:1.5em; line-height:1.45; word-break:break-all; word-wrap:break-word; color:rgb(51,51,51); border:none; max-height:35em; position:relative; margin-top:0px!important"><code style="font-family:"Source Code Pro",Consolas,Menlo,Monaco,"Courier New",monospace; font-size:1em; padding:0px; color:inherit; background:none; white-space:inherit">您可能感兴趣的文章:
想系统学习GO语言(Golang
请问没有任何编程基础,如何学习GO语言?
Go语言开发必读书目,从菜鸟到大牛必备
Golang 基础教程
GO语言零基础从入门到精通视频教程
Golang语言基础课件
2018年最全Go语言教程零基础入门到进阶实战视频
Go语言发展历史、核心、特性及学习路线
IDEA配置Go语言Go1.5.1开发环境IDEA2020.1.1-Go语言自己动手写Java虚拟机 (Java核心技术系列)
Golang笔记:语法,并发思想,web开发,Go微服务相关

[关闭]
~ ~