提交 34fb8ceb 编写于 作者: 写代码的明哥's avatar 写代码的明哥

update

上级 9d86c317
......@@ -82,7 +82,25 @@ python
go
```
如果 defer 后面跟的是匿名函数,情况会有所不同, defer 会取到最后的变量值
```go
package main
import "fmt"
func main() {
name := "go"
defer func(){
fmt.Println(name) // 输出: python
}()
name = "python"
fmt.Println(name) // 输出: python
}
```
非常抱歉,目前以我的水平,暂时还无法解释这种现象,我建议是单独记忆一下这种特殊场景。
## 3. 多个defer 反序调用
......
......@@ -88,6 +88,26 @@ defer 的用法很简单,只要在后面跟一个函数的调用,就能实
python
go
如果 defer 后面跟的是匿名函数,情况会有所不同, defer 会取到最后的变量值
.. code:: go
package main
import "fmt"
func main() {
name := "go"
defer func(){
fmt.Println(name) // 输出: python
}()
name = "python"
fmt.Println(name) // 输出: python
}
非常抱歉,目前以我的水平,暂时还无法解释这种现象,我建议是单独记忆一下这种特殊场景。
3. 多个defer 反序调用
---------------------
......
......@@ -79,7 +79,7 @@ func main() {
每个包都允许有一个 `init` 函数,当这个包被导入时,会执行该包的这个 `init` 函数,做一些初始化任务。
对于 `init ` 函数的执行有点需要注意
对于 `init ` 函数的执行有点需要注意
1. `init` 函数优先于 `main` 函数执行
......@@ -88,6 +88,41 @@ func main() {
```
C.init→B.init→A.init→main
```
3. 同一个包甚至同一个源文件,可以有多个 init 函数
4. init 函数不能有入参和返回值
5. init 函数不能被其他函数调用
6. 同一个包内的多个 init 顺序是不受保证的
7. 在 init 之前,其实会先初始化包作用域的常量和变量(常量优先于变量),具体可参考如下代码
```go
package main
import "fmt"
func init() {
fmt.Println("init1:", a)
}
func init() {
fmt.Println("init2:", a)
}
var a = 10
const b = 100
func main() {
fmt.Println("main:", a)
}
// 执行结果
// init1: 10
// init2: 10
// main: 10
```
......
......@@ -87,7 +87,7 @@ fmt,那每次使用它的打印函数打印时,都要 包名+方法名。
每个包都允许有一个 ``init`` 函数,当这个包被导入时,会执行该包的这个
``init`` 函数,做一些初始化任务。
对于 ``init`` 函数的执行有点需要注意
对于 ``init`` 函数的执行有点需要注意
1. ``init`` 函数优先于 ``main`` 函数执行
......@@ -97,6 +97,42 @@ fmt,那每次使用它的打印函数打印时,都要 包名+方法名。
C.initB.initA.initmain
3. 同一个包甚至同一个源文件,可以有多个 init 函数
4. init 函数不能有入参和返回值
5. init 函数不能被其他函数调用
6. 同一个包内的多个 init 顺序是不受保证的
7. init
之前,其实会先初始化包作用域的常量和变量(常量优先于变量),具体可参考如下代码
.. code:: go
package main
import "fmt"
func init() {
fmt.Println("init1:", a)
}
func init() {
fmt.Println("init2:", a)
}
var a = 10
const b = 100
func main() {
fmt.Println("main:", a)
}
// 执行结果
// init1: 10
// init2: 10
// main: 10
5. 包的匿名导入
---------------
......
......@@ -73,7 +73,7 @@ func main() {
// output: 6
```
其中 `...` 是 Go 语言为了方便程序员写代码而实现的语法糖,如果该函数下多个类型的参数,这个语法糖必须得是最后一个参数。
其中 `...` 是 Go 语言为了方便程序员写代码而实现的语法糖,如果该函数下多个类型的参数,这个语法糖必须得是最后一个参数。
同时这个语法糖,只能在定义函数时使用。
......@@ -192,7 +192,7 @@ Go语言中的函数,在你定义的时候,就规定了此函数
## 6. 方法与函数
方法,在上一节[2.1 面向对象:结构体与继承](http://golang.iswbm.com/en/latest/c02/c02_01.html)》里已经介绍过了,它的定义与函数有些不同,你可以点击前面的标题进行交叉学习。
方法,在之前的[2.1 面向对象:结构体与继承](http://golang.iswbm.com/en/latest/c02/c02_01.html)》里已经介绍过了,它的定义与函数有些不同,你可以点击前面的标题进行交叉学习。
**方法和函数有什么区别?** 为防会有朋友第一次接触面向对象,这里多嘴一句。
......
......@@ -86,7 +86,7 @@ a,b,并规定必须返回一个int类型的值 。
// output: 6
其中 ``...`` 是 Go
语言为了方便程序员写代码而实现的语法糖,如果该函数下多个类型的参数,这个语法糖必须得是最后一个参数。
语言为了方便程序员写代码而实现的语法糖,如果该函数下多个类型的参数,这个语法糖必须得是最后一个参数。
同时这个语法糖,只能在定义函数时使用。
......@@ -204,7 +204,7 @@ Go语言中的函数,在你定义的时候,就规定了此函数
6. 方法与函数
-------------
方法,在上一节《\ `2.1
方法,在之前的《\ `2.1
面向对象:结构体与继承 <http://golang.iswbm.com/en/latest/c02/c02_01.html>`__\ 》里已经介绍过了,它的定义与函数有些不同,你可以点击前面的标题进行交叉学习。
那 **方法和函数有什么区别?**
......
......@@ -129,6 +129,14 @@ W3Cschool 也是一个专业的编程入门学习及技术文档查询应用,
![](http://image.iswbm.com/20200430112319.png)
### Go语言101
《Go语言101》是一本着墨于Go语法语义以及运行时相关知识点的编程指导书(Go 1.16就绪)。 此书旨在尽可能地帮助Go程序员更深更全面地理解Go语言。 此书也搜集了Go语言和Go编程中的很多细节。 此书同时适合Go初学者和有一定经验的Go程序员阅读。
**网站链接**:https://gfw.go101.org/article/101.html
![](http://image.iswbm.com/image-20210411181553375.png)
## 2. Web开发
### gin 中文文档
......
......@@ -141,6 +141,18 @@ Go by Example
|image11|
Go语言101
~~~~~~~~~
《Go语言101》是一本着墨于Go语法语义以及运行时相关知识点的编程指导书(Go
1.16就绪)。 此书旨在尽可能地帮助Go程序员更深更全面地理解Go语言。
此书也搜集了Go语言和Go编程中的很多细节。
此书同时适合Go初学者和有一定经验的Go程序员阅读。
**网站链接**\ :https://gfw.go101.org/article/101.html
|image12|
2. Web开发
----------
......@@ -184,7 +196,7 @@ Revel 中文文档
**网站链接**\ :https://eddycjy.gitbook.io/golang/
|image12|
|image13|
Go语言圣经
~~~~~~~~~~
......@@ -194,7 +206,7 @@ Go语言圣经
**网站链接**\ :https://books.studygolang.com/gopl-zh/
|image13|
|image14|
mojotv 进阶系列
~~~~~~~~~~~~~~~
......@@ -205,14 +217,14 @@ mojotv 进阶系列
**网站链接**\ :https://mojotv.cn/404#Golang
|image14|
|image15|
Go 语言高级编程
~~~~~~~~~~~~~~~
**网站链接**\ :https://chai2010.gitbooks.io/advanced-go-programming-book/content/
|image15|
|image16|
4. 工具使用
-----------
......@@ -228,7 +240,7 @@ go 的命令非常多,如果想系统的学习,推荐郝林的 Go 命令教
2、https://wiki.jikexueyuan.com/project/go-command-tutorial/0.0.html
|image16|
|image17|
Uber 编程规范
~~~~~~~~~~~~~
......@@ -245,7 +257,7 @@ Uber”,也就是 Go 代码应该怎样写、不该怎样写。
中文译文:https://www.infoq.cn/article/G6c95VyU5telNXXCC9yO
|image17|
|image18|
Go Walker
~~~~~~~~~
......@@ -253,7 +265,7 @@ Go Walker
Go Walker 是一个可以在线生成并浏览 `Go <https://golang.org/>`__ 项目 API
文档的 Web 服务器,目前已支持包括 **GitHub** 等代码托管平台。
|image18|
|image19|
CTOLib 码库
~~~~~~~~~~~
......@@ -263,7 +275,7 @@ CTOLib 码库
**网站链接**\ :https://www.ctolib.com/go/categories/go-guide.html
|image19|
|image20|
5. 技术社区
-----------
......@@ -273,7 +285,7 @@ GoCN
**网站链接**\ :https://gocn.vip/
|image20|
|image21|
Go 语言中文网
~~~~~~~~~~~~~
......@@ -284,7 +296,7 @@ Go历史版本的下载,各种高质量的电子书资源,各种大牛写的
**网站链接**\ :https://studygolang.com/
|image21|
|image22|
6. 源码学习
-----------
......@@ -297,14 +309,14 @@ Go 夜读
**网站链接**\ :https://talkgo.org/
|image22|
|image23|
Go 语言原本
~~~~~~~~~~~
**网站链接**\ :https://changkun.de/golang/
|image23|
|image24|
Go 语言设计与实现
~~~~~~~~~~~~~~~~~
......@@ -313,14 +325,14 @@ Go 语言设计与实现
**网站链接**\ :https://draveness.me/golang/
|image24|
|image25|
能翻到这里的,一定是真爱了,本以为 Go
语言还处于不温不火的状态,没想到收集整理一下,资料还挺多的。
--------------
|image25|
|image26|
.. |image0| image:: http://image.iswbm.com/20200607145423.png
.. |image1| image:: http://image.iswbm.com/20200430112024.png
......@@ -334,18 +346,19 @@ Go 语言设计与实现
.. |image9| image:: http://image.iswbm.com/20200430104324.png
.. |image10| image:: http://image.iswbm.com/20200430174507.png
.. |image11| image:: http://image.iswbm.com/20200430112319.png
.. |image12| image:: http://image.iswbm.com/20200430105116.png
.. |image13| image:: http://image.iswbm.com/20200430100755.png
.. |image14| image:: http://image.iswbm.com/20200430095544.png
.. |image15| image:: http://image.iswbm.com/20200430175818.png
.. |image16| image:: http://image.iswbm.com/20200430102821.png
.. |image17| image:: http://image.iswbm.com/20200430113756.png
.. |image18| image:: http://image.iswbm.com/20200430170054.png
.. |image19| image:: http://image.iswbm.com/20200430174109.png
.. |image20| image:: http://image.iswbm.com/20200506192127.png
.. |image21| image:: http://image.iswbm.com/20200430134207.png
.. |image22| image:: http://image.iswbm.com/20200430174216.png
.. |image23| image:: http://image.iswbm.com/20200506191803.png
.. |image24| image:: http://image.iswbm.com/20200506191632.png
.. |image25| image:: http://image.iswbm.com/20200607174235.png
.. |image12| image:: http://image.iswbm.com/image-20210411181553375.png
.. |image13| image:: http://image.iswbm.com/20200430105116.png
.. |image14| image:: http://image.iswbm.com/20200430100755.png
.. |image15| image:: http://image.iswbm.com/20200430095544.png
.. |image16| image:: http://image.iswbm.com/20200430175818.png
.. |image17| image:: http://image.iswbm.com/20200430102821.png
.. |image18| image:: http://image.iswbm.com/20200430113756.png
.. |image19| image:: http://image.iswbm.com/20200430170054.png
.. |image20| image:: http://image.iswbm.com/20200430174109.png
.. |image21| image:: http://image.iswbm.com/20200506192127.png
.. |image22| image:: http://image.iswbm.com/20200430134207.png
.. |image23| image:: http://image.iswbm.com/20200430174216.png
.. |image24| image:: http://image.iswbm.com/20200506191803.png
.. |image25| image:: http://image.iswbm.com/20200506191632.png
.. |image26| image:: http://image.iswbm.com/20200607174235.png
......@@ -4,7 +4,7 @@
## 1. 什么是边界检查?
边界检查,英文名 `Bounds Check Elimination`,简称为 BCE。它是 Go 语言中防止数组、切片越界的检查手段。如果检查下标已经越界了,就会产生 Panic。
边界检查,英文名 `Bounds Check Elimination`,简称为 BCE。它是 Go 语言中防止数组、切片越界而导致内存不安全的检查手段。如果检查下标已经越界了,就会产生 Panic。
边界检查使得我们的代码能够安全地运行,但是另一方面,也使得我们的代码运行效率略微降低。
......@@ -130,7 +130,7 @@ import (
"math/rand"
)
func fa() {
func f() {
s := make([]int, 3, 5)
index := rand.Intn(3)
_ = s[:index] // 第一次检查
......@@ -157,7 +157,7 @@ func main() {}
### 3.4 案例四
有了上面的铺垫,再来看下面这个示例,由于数组是调用者传入的参数,所以编译器的编译的时候无法得知数组的长度和容量是否相等,因只能保险一点,两个都检查。
有了上面的铺垫,再来看下面这个示例,由于数组是调用者传入的参数,所以编译器的编译的时候无法得知数组的长度和容量是否相等,因只能保险一点,两个都检查。
```go
package main
......@@ -166,7 +166,7 @@ import (
"math/rand"
)
func fb(s []int, index int) {
func f(s []int, index int) {
_ = s[:index] // 第一次检查
_ = s[index:] // 第二次检查
}
......@@ -183,7 +183,7 @@ import (
"math/rand"
)
func fb(s []int, index int) {
func f(s []int, index int) {
_ = s[index:] // 第一次检查
_ = s[:index] // 不用检查
}
......@@ -203,7 +203,7 @@ func main() {}
package main
func fd(is []int, bs []byte) {
func f(is []int, bs []byte) {
if len(is) >= 256 {
for _, n := range bs {
_ = is[n] // 每个循环都要边界检查
......@@ -219,7 +219,7 @@ func main() {}
package main
func fd(is []int, bs []byte) {
func f(is []int, bs []byte) {
if len(is) >= 256 {
is = is[:256]
for _, n := range bs {
......
......@@ -7,7 +7,7 @@
-------------------
边界检查,英文名 ``Bounds Check Elimination``\ ,简称为 BCE。它是 Go
语言中防止数组、切片越界的检查手段。如果检查下标已经越界了,就会产生
语言中防止数组、切片越界而导致内存不安全的检查手段。如果检查下标已经越界了,就会产生
Panic
边界检查使得我们的代码能够安全地运行,但是另一方面,也使得我们的代码运行效率略微降低。
......@@ -146,7 +146,7 @@ panic。
"math/rand"
)
func fa() {
func f() {
s := make([]int, 3, 5)
index := rand.Intn(3)
_ = s[:index] // 第一次检查
......@@ -179,7 +179,7 @@ panic。
3.4 案例四
~~~~~~~~~~
有了上面的铺垫,再来看下面这个示例,由于数组是调用者传入的参数,所以编译器的编译的时候无法得知数组的长度和容量是否相等,因只能保险一点,两个都检查。
有了上面的铺垫,再来看下面这个示例,由于数组是调用者传入的参数,所以编译器的编译的时候无法得知数组的长度和容量是否相等,因只能保险一点,两个都检查。
.. code:: go
......@@ -189,7 +189,7 @@ panic。
"math/rand"
)
func fb(s []int, index int) {
func f(s []int, index int) {
_ = s[:index] // 第一次检查
_ = s[index:] // 第二次检查
}
......@@ -206,7 +206,7 @@ panic。
"math/rand"
)
func fb(s []int, index int) {
func f(s []int, index int) {
_ = s[index:] // 第一次检查
_ = s[:index] // 不用检查
}
......@@ -227,7 +227,7 @@ panic。
package main
func fd(is []int, bs []byte) {
func f(is []int, bs []byte) {
if len(is) >= 256 {
for _, n := range bs {
_ = is[n] // 每个循环都要边界检查
......@@ -245,7 +245,7 @@ panic。
package main
func fd(is []int, bs []byte) {
func f(is []int, bs []byte) {
if len(is) >= 256 {
is = is[:256]
for _, n := range bs {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册