Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
go开源项目镜像
GolangCodingTime
提交
10ad0b50
G
GolangCodingTime
项目概览
go开源项目镜像
/
GolangCodingTime
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
GolangCodingTime
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
10ad0b50
编写于
3月 30, 2020
作者:
王
王炳明
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add:新增文章<类型断言>
上级
205a7e59
变更
35
隐藏空白更改
内联
并排
Showing
35 changed file
with
393 addition
and
48 deletion
+393
-48
README.md
README.md
+1
-1
md2rst.py
md2rst.py
+3
-3
source/c01/c01_01.rst
source/c01/c01_01.rst
+1
-2
source/c01/c01_02.rst
source/c01/c01_02.rst
+0
-1
source/c01/c01_03.rst
source/c01/c01_03.rst
+0
-1
source/c01/c01_04.rst
source/c01/c01_04.rst
+0
-1
source/c01/c01_05.rst
source/c01/c01_05.rst
+0
-1
source/c01/c01_06.rst
source/c01/c01_06.rst
+0
-1
source/c01/c01_07.rst
source/c01/c01_07.rst
+0
-1
source/c01/c01_08.rst
source/c01/c01_08.rst
+0
-1
source/c01/c01_09.rst
source/c01/c01_09.rst
+0
-1
source/c01/c01_10.rst
source/c01/c01_10.rst
+0
-1
source/c01/c01_11.rst
source/c01/c01_11.rst
+0
-1
source/c01/c01_12.rst
source/c01/c01_12.rst
+0
-1
source/c01/c01_13.rst
source/c01/c01_13.rst
+0
-1
source/c01/c01_14.md
source/c01/c01_14.md
+185
-5
source/c01/c01_14.rst
source/c01/c01_14.rst
+201
-6
source/c02/c02_01.rst
source/c02/c02_01.rst
+0
-1
source/c02/c02_02.md
source/c02/c02_02.md
+1
-1
source/c02/c02_02.rst
source/c02/c02_02.rst
+0
-1
source/c02/c02_03.rst
source/c02/c02_03.rst
+0
-1
source/c02/c02_04.rst
source/c02/c02_04.rst
+0
-1
source/c03/c03_01.rst
source/c03/c03_01.rst
+0
-1
source/c03/c03_02.rst
source/c03/c03_02.rst
+0
-1
source/c03/c03_03.rst
source/c03/c03_03.rst
+0
-1
source/c03/c03_04.rst
source/c03/c03_04.rst
+0
-1
source/c03/c03_05.rst
source/c03/c03_05.rst
+0
-1
source/c04/c04_01.rst
source/c04/c04_01.rst
+0
-1
source/c04/c04_02.rst
source/c04/c04_02.rst
+0
-1
source/c04/c04_03.rst
source/c04/c04_03.rst
+1
-2
source/c04/c04_04.rst
source/c04/c04_04.rst
+0
-1
source/c04/c04_05.rst
source/c04/c04_05.rst
+0
-1
source/c04/c04_06.rst
source/c04/c04_06.rst
+0
-1
source/c04/c04_08.rst
source/c04/c04_08.rst
+0
-1
source/c05/c05_01.rst
source/c05/c05_01.rst
+0
-1
未找到文件。
README.md
浏览文件 @
10ad0b50
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
-
1.11
[
流程控制:goto 无条件跳转
](
http://golang.iswbm.com/en/latest/c01/c01_11.html
)
-
1.11
[
流程控制:goto 无条件跳转
](
http://golang.iswbm.com/en/latest/c01/c01_11.html
)
-
1.12
[
流程控制:defer 延迟语句
](
http://golang.iswbm.com/en/latest/c01/c01_12.html
)
-
1.12
[
流程控制:defer 延迟语句
](
http://golang.iswbm.com/en/latest/c01/c01_12.html
)
-
1.13
[
异常机制:panic 和 recover
](
http://golang.iswbm.com/en/latest/c01/c01_13.html
)
-
1.13
[
异常机制:panic 和 recover
](
http://golang.iswbm.com/en/latest/c01/c01_13.html
)
-
1.14
[
Type Assertion 与 Type Switch
](
http://golang.iswbm.com/en/latest/c01/c01_14.html
)
-
1.14
[
Go 语言中的类型断言?
](
http://golang.iswbm.com/en/latest/c01/c01_14.html
)
-
1.15
[
信道里使用 select
](
http://golang.iswbm.com/en/latest/c01/c01_15.html
)
-
1.15
[
信道里使用 select
](
http://golang.iswbm.com/en/latest/c01/c01_15.html
)
## 第二章:面向对象
## 第二章:面向对象
...
...
md2rst.py
浏览文件 @
10ad0b50
...
@@ -10,9 +10,9 @@ from git import Repo
...
@@ -10,9 +10,9 @@ from git import Repo
osName
=
platform
.
system
()
osName
=
platform
.
system
()
repo_path
=
'.'
repo_path
=
'.'
if
(
osName
==
'Windows'
):
if
(
osName
==
'Windows'
):
repo_path
=
'E:
\\
MING-Git
\\
07.
GolangCodingTime'
repo_path
=
'E:
\\
MING-Git
\\
GolangCodingTime'
blog_path
=
'E:
\\
MING-Git
\
\
07.
GolangCodingTime
\\
source'
blog_path
=
'E:
\\
MING-Git\GolangCodingTime
\\
source'
index_path
=
'E:
\\
MING-Git
\\
07.
GolangCodingTime
\\
README.md'
index_path
=
'E:
\\
MING-Git
\\
GolangCodingTime
\\
README.md'
elif
(
osName
==
'Darwin'
):
elif
(
osName
==
'Darwin'
):
repo_path
=
'/Users/MING/Github/GolangCodingTime/'
repo_path
=
'/Users/MING/Github/GolangCodingTime/'
blog_path
=
'/Users/MING/Github/GolangCodingTime/source'
blog_path
=
'/Users/MING/Github/GolangCodingTime/source'
...
...
source/c01/c01_01.rst
浏览文件 @
10ad0b50
...
@@ -57,7 +57,7 @@ Goland 下载地址:https://download.jetbrains.com/go/goland-2019.2.3.exe
...
@@ -57,7 +57,7 @@ Goland 下载地址:https://download.jetbrains.com/go/goland-2019.2.3.exe
- 激活码.txt:激活码
- 激活码.txt:激活码
将 jetbrains-agent.jar 拷贝到 你的 Goland
将 jetbrains-agent.jar 拷贝到 你的 Goland
安装目录的bin文件夹下,我的路径是:E::raw-latex:`\Program `Files:raw-latex:`\JetBrains`:raw-latex:`\GoLand
`2019.2
.3:raw-latex:`\bin`
安装目录的bin文件夹下,我的路径是:E::raw-latex:`\Program `Files:raw-latex:`\JetBrains`:raw-latex:`\GoLand
2019.2`
.3:raw-latex:`\bin`
然后用编辑器打开这两个文件
然后用编辑器打开这两个文件
...
@@ -314,4 +314,3 @@ Code,其记录的GOPATH始终指向%USERPROFILE%:raw-latex:`\go`),
...
@@ -314,4 +314,3 @@ Code,其记录的GOPATH始终指向%USERPROFILE%:raw-latex:`\go`),
.. |image24| image:: http://image.python-online.cn/20200109214117.png
.. |image24| image:: http://image.python-online.cn/20200109214117.png
.. |image25| image:: http://image.python-online.cn/20200109154657.png
.. |image25| image:: http://image.python-online.cn/20200109154657.png
.. |image26| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image26| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_02.rst
浏览文件 @
10ad0b50
...
@@ -207,4 +207,3 @@ float64,但是很多情况下,我们并不需要这么高的精度(占用
...
@@ -207,4 +207,3 @@ float64,但是很多情况下,我们并不需要这么高的精度(占用
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c01/c01_03.rst
浏览文件 @
10ad0b50
...
@@ -233,4 +233,3 @@ https://www.zhihu.com/question/26022206
...
@@ -233,4 +233,3 @@ https://www.zhihu.com/question/26022206
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
20200120204329.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
20200120204329.
png
..
|
image1
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image1
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c01/c01_04.rst
浏览文件 @
10ad0b50
...
@@ -243,4 +243,3 @@ byte数组
...
@@ -243,4 +243,3 @@ byte数组
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_05.rst
浏览文件 @
10ad0b50
...
@@ -232,4 +232,3 @@
...
@@ -232,4 +232,3 @@
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_06.rst
浏览文件 @
10ad0b50
...
@@ -292,4 +292,3 @@ Go 中确实不如 Python 那样灵活,bool 与 int
...
@@ -292,4 +292,3 @@ Go 中确实不如 Python 那样灵活,bool 与 int
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
20200106201856.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
20200106201856.
png
..
|
image1
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image1
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c01/c01_07.rst
浏览文件 @
10ad0b50
...
@@ -220,4 +220,3 @@ ptr(pointer的简写),而这个变量,我们称之为指针变量。
...
@@ -220,4 +220,3 @@ ptr(pointer的简写),而这个变量,我们称之为指针变量。
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c01/c01_08.rst
浏览文件 @
10ad0b50
...
@@ -126,4 +126,3 @@ if - else if - else
...
@@ -126,4 +126,3 @@ if - else if - else
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_09.rst
浏览文件 @
10ad0b50
...
@@ -258,4 +258,3 @@ default 的代码块。
...
@@ -258,4 +258,3 @@ default 的代码块。
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_10.rst
浏览文件 @
10ad0b50
...
@@ -174,4 +174,3 @@ range 后可接数组、切片,字符串等
...
@@ -174,4 +174,3 @@ range 后可接数组、切片,字符串等
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_11.rst
浏览文件 @
10ad0b50
...
@@ -176,4 +176,3 @@ goto语句与标签之间不能有变量声明,否则编译错误。
...
@@ -176,4 +176,3 @@ goto语句与标签之间不能有变量声明,否则编译错误。
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_12.rst
浏览文件 @
10ad0b50
...
@@ -238,4 +238,3 @@ return 那里执行不就好了。
...
@@ -238,4 +238,3 @@ return 那里执行不就好了。
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c01/c01_13.rst
浏览文件 @
10ad0b50
...
@@ -151,4 +151,3 @@ recover,并不能任意使用,它有强制要求,必须得在 defer 下才
...
@@ -151,4 +151,3 @@ recover,并不能任意使用,它有强制要求,必须得在 defer 下才
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c01/c01_14.md
浏览文件 @
10ad0b50
# 1.14
Type Assertion 与 Type Switch
# 1.14
Go 语言中的类型断言?
## Type Assertion
Type Assertion(中文名叫:类型断言),通过它可以做到以下几件事情
https://sanyuesha.com/2017/12/01/go-interface-4/
1.
检查
`i`
是否为 nil
2.
检查
`i`
存储的值是否为某个类型
https://maiyang.me/post/2018-08-30-type-assertion-switch/
具体的使用方式有两种:
https://www.kancloud.cn/hartnett/gopl-zh/126063
**第一种:**
https://segmentfault.com/a/1190000016230264
```
\ No newline at end of file
t := i.(T)
```
这个表达式可以断言一个接口对象(i)里不是 nil,并且接口对象(i)存储的值的类型是 T,如果断言成功,就会返回值给 t,如果断言失败,就会触发 panic。
来写段代码试验一下
```
go
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
10
t1
:=
i
.
(
int
)
fmt
.
Println
(
t1
)
fmt
.
Println
(
"=====分隔线====="
)
t2
:=
i
.
(
string
)
fmt
.
Println
(
t2
)
}
```
运行后输出如下,可以发现在执行第二次断言的时候失败了,并且触发了 panic
```
10
=====分隔线=====
panic: interface conversion: interface {} is int, not string
goroutine 1 [running]:
main.main()
E:/GoPlayer/src/main.go:12 +0x10e
exit status 2
```
如果要断言的接口值是 nil,那我们来看看也是不是也如预期一样会触发panic
```
go
package
main
func
main
()
{
var
i
interface
{}
// nil
var
_
=
i
.
(
interface
{})
}
```
输出如下,确实是会 触发 panic
```
panic: interface conversion: interface is nil, not interface {}
goroutine 1 [running]:
main.main()
E:/GoPlayer/src/main.go:5 +0x34
exit status 2
```
**第二种**
```
t, ok:= i.(T)
```
和上面一样,这个表达式也是可以断言一个接口对象(i)里不是 nil,并且接口对象(i)存储的值的类型是 T,如果断言成功,就会返回其类型给 t,并且此时 ok 的值 为 true,表示断言成功。
如果接口值的类型,并不是我们所断言的 T,就会断言失败,但和第一种表达式不同的事,这个不会触发 panic,而是将 ok 的值设为 false ,表示断言失败,此时t 为 T 的零值。
稍微修改下上面的例子,如下
```
go
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
10
t1
,
ok
:=
i
.
(
int
)
fmt
.
Printf
(
"%d-%t
\n
"
,
t1
,
ok
)
fmt
.
Println
(
"=====分隔线1====="
)
t2
,
ok
:=
i
.
(
string
)
fmt
.
Printf
(
"%s-%t
\n
"
,
t2
,
ok
)
fmt
.
Println
(
"=====分隔线2====="
)
var
k
interface
{}
// nil
t3
,
ok
:=
k
.
(
interface
{})
fmt
.
Println
(
t3
,
"-"
,
ok
)
fmt
.
Println
(
"=====分隔线3====="
)
k
=
10
t4
,
ok
:=
k
.
(
interface
{})
fmt
.
Printf
(
"%d-%t
\n
"
,
t4
,
ok
)
t5
,
ok
:=
k
.
(
int
)
fmt
.
Printf
(
"%d-%t
\n
"
,
t5
,
ok
)
}
```
运行后输出如下,可以发现在执行第二次断言的时候,虽然失败了,但并没有触发了 panic。
```
10-true
=====分隔线1=====
-false
=====分隔线2=====
<nil> - false
=====分隔线3=====
10-true
10-true
```
上面这段输出,你要注意的是第二个断言的输出在
`-false`
之前并不是有没有输出任何 t2 的值,而是由于断言失败,所以 t2 得到的是 string 的零值也是
`""`
,它是零长度的,所以你看不到其输出。
## Type Switch
如果需要区分多种类型,可以使用 type switch 断言,这个将会比一个一个进行类型断言更简单、直接、高效。
```
go
package
main
import
"fmt"
func
findType
(
i
interface
{})
{
switch
x
:=
i
.
(
type
)
{
case
int
:
fmt
.
Println
(
x
,
"is int"
)
case
string
:
fmt
.
Println
(
x
,
"is string"
)
case
nil
:
fmt
.
Println
(
x
,
"is nil"
)
default
:
fmt
.
Println
(
x
,
"not type matched"
)
}
}
func
main
()
{
findType
(
10
)
// int
findType
(
"hello"
)
// string
var
k
interface
{}
// nil
findType
(
k
)
findType
(
10.23
)
//float64
}
```
输出如下
```
10 is int
hello is string
<nil> is nil
10.23 not type matched
```
额外说明一下:
-
如果你的值是 nil,那么匹配的是
`case nil`
-
如果你的值在 switch-case 里并没有匹配对应的类型,那么走的是 default 分支
## 参考文章
-
[
Explain Type Assertions in Go
](
https://stackoverflow.com/questions/38816843/explain-type-assertions-in-go
)
-
[
Go interface 详解 (四) :类型断言
](
https://sanyuesha.com/2017/12/01/go-interface-4/
)
---
![](
http://image.python-online.cn/image-20200320125724880.png
)
\ No newline at end of file
source/c01/c01_14.rst
浏览文件 @
10ad0b50
1.14
Type Assertion 与 Type Switch
1.14
Go
语言中的类型断言?
==========================
========
==========================
https://sanyuesha.com/2017/12/01/go-interface-4/
Type
Assertion
--------------
https://maiyang.me/post/2018-08-30-type-assertion-switch/
Type
Assertion
(中文名叫:类型断言),通过它可以做到以下几件事情
https://www.kancloud.cn/hartnett/gopl-zh/126063
1.
检查
``
i
``
是否为
nil
2.
检查
``
i
``
存储的值是否为某个类型
https://segmentfault.com/a/1190000016230264
具体的使用方式有两种:
**
第一种:
**
::
t
:=
i
.(
T
)
这个表达式可以断言一个接口对象(
i
)里不是
nil
,并且接口对象(
i
)存储的值的类型是
T
,如果断言成功,就会返回值给
t
,如果断言失败,就会触发
panic
。
来写段代码试验一下
..
code
::
go
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
10
t1
:=
i
.(
int
)
fmt
.
Println
(
t1
)
fmt
.
Println
(
"=====分隔线====="
)
t2
:=
i
.(
string
)
fmt
.
Println
(
t2
)
}
运行后输出如下,可以发现在执行第二次断言的时候失败了,并且触发了
panic
::
10
=====
分隔线
=====
panic
:
interface
conversion
:
interface
{}
is
int
,
not
string
goroutine
1
[
running
]:
main
.
main
()
E
:/
GoPlayer
/
src
/
main
.
go
:
12
+
0x10e
exit
status
2
如果要断言的接口值是
nil
,那我们来看看也是不是也如预期一样会触发
panic
..
code
::
go
package
main
func
main
()
{
var
i
interface
{}
//
nil
var
_
=
i
.(
interface
{})
}
输出如下,确实是会
触发
panic
::
panic
:
interface
conversion
:
interface
is
nil
,
not
interface
{}
goroutine
1
[
running
]:
main
.
main
()
E
:/
GoPlayer
/
src
/
main
.
go
:
5
+
0x34
exit
status
2
**
第二种
**
::
t
,
ok
:=
i
.(
T
)
和上面一样,这个表达式也是可以断言一个接口对象(
i
)里不是
nil
,并且接口对象(
i
)存储的值的类型是
T
,如果断言成功,就会返回其类型给
t
,并且此时
ok
的值
为
true
,表示断言成功。
如果接口值的类型,并不是我们所断言的
T
,就会断言失败,但和第一种表达式不同的事,这个不会触发
panic
,而是将
ok
的值设为
false
,表示断言失败,此时
t
为
T
的零值。
稍微修改下上面的例子,如下
..
code
::
go
package
main
import
"fmt"
func
main
()
{
var
i
interface
{}
=
10
t1
,
ok
:=
i
.(
int
)
fmt
.
Printf
(
"%d-%t
\n
"
,
t1
,
ok
)
fmt
.
Println
(
"=====分隔线1====="
)
t2
,
ok
:=
i
.(
string
)
fmt
.
Printf
(
"%s-%t
\n
"
,
t2
,
ok
)
fmt
.
Println
(
"=====分隔线2====="
)
var
k
interface
{}
//
nil
t3
,
ok
:=
k
.(
interface
{})
fmt
.
Println
(
t3
,
"-"
,
ok
)
fmt
.
Println
(
"=====分隔线3====="
)
k
=
10
t4
,
ok
:=
k
.(
interface
{})
fmt
.
Printf
(
"%d-%t
\n
"
,
t4
,
ok
)
t5
,
ok
:=
k
.(
int
)
fmt
.
Printf
(
"%d-%t
\n
"
,
t5
,
ok
)
}
运行后输出如下,可以发现在执行第二次断言的时候,虽然失败了,但并没有触发了
panic
。
::
10
-
true
=====
分隔线
1
=====
-
false
=====
分隔线
2
=====
<
nil
>
-
false
=====
分隔线
3
=====
10
-
true
10
-
true
上面这段输出,你要注意的是第二个断言的输出在
\
``-
false
``
之前并不是有没有输出任何
t2
的值,而是由于断言失败,所以
t2
得到的是
string
的零值也是
``
""
``
,它是零长度的,所以你看不到其输出。
Type
Switch
-----------
如果需要区分多种类型,可以使用
type
switch
断言,这个将会比一个一个进行类型断言更简单、直接、高效。
..
code
::
go
package
main
import
"fmt"
func
findType
(
i
interface
{})
{
switch
x
:=
i
.(
type
)
{
case
int
:
fmt
.
Println
(
x
,
"is int"
)
case
string
:
fmt
.
Println
(
x
,
"is string"
)
case
nil
:
fmt
.
Println
(
x
,
"is nil"
)
default
:
fmt
.
Println
(
x
,
"not type matched"
)
}
}
func
main
()
{
findType
(
10
)
//
int
findType
(
"hello"
)
//
string
var
k
interface
{}
//
nil
findType
(
k
)
findType
(
10.23
)
//
float64
}
输出如下
::
10
is
int
hello
is
string
<
nil
>
is
nil
10.23
not
type
matched
额外说明一下:
-
如果你的值是
nil
,那么匹配的是
``
case
nil
``
-
如果你的值在
switch
-
case
里并没有匹配对应的类型,那么走的是
default
分支
参考文章
--------
-
`
Explain
Type
Assertions
in
Go
<
https
://
stackoverflow
.
com
/
questions
/
38816843
/
explain
-
type
-
assertions
-
in
-
go
>`
__
-
`
Go
interface
详解
(
四
)
:类型断言
<
https
://
sanyuesha
.
com
/
2017
/
12
/
01
/
go
-
interface
-
4
/>`
__
--------------
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c02/c02_01.rst
浏览文件 @
10ad0b50
...
@@ -268,4 +268,3 @@ staff 中,做为 staff 的一个匿名字段,staff 就直接拥有了 compan
...
@@ -268,4 +268,3 @@ staff 中,做为 staff 的一个匿名字段,staff 就直接拥有了 compan
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c02/c02_02.md
浏览文件 @
10ad0b50
...
@@ -28,7 +28,7 @@ type Phone interface {
...
@@ -28,7 +28,7 @@ type Phone interface {
这个接口的实现是隐式的,不像 JAVA 中要用 implements 显示说明。
这个接口的实现是隐式的,不像 JAVA 中要用 implements 显示说明。
继续上面电话的例子,我们先定义一个 Nokia 的结构体,而它实现了 call 的方法,所以它也是一台电话。
继续上面电话的例子,我们先定义一个 Nokia 的结构体,而它实现了 call 的方法,所以它也是一台电话。
```
go
```
go
type
Nokia
struct
{
type
Nokia
struct
{
...
...
source/c02/c02_02.rst
浏览文件 @
10ad0b50
...
@@ -242,4 +242,3 @@ typing)的定义是,只要你长得像鸭子,叫起来也像鸭子,那
...
@@ -242,4 +242,3 @@ typing)的定义是,只要你长得像鸭子,叫起来也像鸭子,那
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c02/c02_03.rst
浏览文件 @
10ad0b50
...
@@ -101,4 +101,3 @@ make:只能为 slice,map,chan 分配内存,并初始化,返回的是
...
@@ -101,4 +101,3 @@ make:只能为 slice,map,chan 分配内存,并初始化,返回的是
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c02/c02_04.rst
浏览文件 @
10ad0b50
...
@@ -158,4 +158,3 @@ name
...
@@ -158,4 +158,3 @@ name
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c03/c03_01.rst
浏览文件 @
10ad0b50
...
@@ -323,4 +323,3 @@ go vender 械管理,除此之外,任何 Gopher 应该从此刻就投入 go m
...
@@ -323,4 +323,3 @@ go vender 械管理,除此之外,任何 Gopher 应该从此刻就投入 go m
.. |image11| image:: http://image.python-online.cn/image-20200314005924877.png
.. |image11| image:: http://image.python-online.cn/image-20200314005924877.png
.. |image12| image:: http://image.python-online.cn/image-20200314005217447.png
.. |image12| image:: http://image.python-online.cn/image-20200314005217447.png
.. |image13| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image13| image:: http://image.python-online.cn/image-20200320125724880.png
source/c03/c03_02.rst
浏览文件 @
10ad0b50
...
@@ -182,4 +182,3 @@ MyBlog的入口应当为 myblog.go
...
@@ -182,4 +182,3 @@ MyBlog的入口应当为 myblog.go
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c03/c03_03.rst
浏览文件 @
10ad0b50
...
@@ -253,4 +253,3 @@ https://juejin.im/post/5d0b865c6fb9a07f050a6f45
...
@@ -253,4 +253,3 @@ https://juejin.im/post/5d0b865c6fb9a07f050a6f45
.. |image10| image:: http://image.python-online.cn/image-20200312221011685.png
.. |image10| image:: http://image.python-online.cn/image-20200312221011685.png
.. |image11| image:: http://image.python-online.cn/image-20200312221141028.png
.. |image11| image:: http://image.python-online.cn/image-20200312221141028.png
.. |image12| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image12| image:: http://image.python-online.cn/image-20200320125724880.png
source/c03/c03_04.rst
浏览文件 @
10ad0b50
...
@@ -277,4 +277,3 @@ pkg,由于这个包是我随便指定的,当然会找不到,找不到就
...
@@ -277,4 +277,3 @@ pkg,由于这个包是我随便指定的,当然会找不到,找不到就
..
|
image5
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200319222834534.
png
..
|
image5
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200319222834534.
png
..
|
image6
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200319225219195.
png
..
|
image6
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200319225219195.
png
..
|
image7
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image7
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c03/c03_05.rst
浏览文件 @
10ad0b50
...
@@ -150,4 +150,3 @@ release 发布一个版本
...
@@ -150,4 +150,3 @@ release 发布一个版本
..
|
image8
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200321132052173.
png
..
|
image8
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200321132052173.
png
..
|
image9
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200321133247067.
png
..
|
image9
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200321133247067.
png
..
|
image10
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image10
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c04/c04_01.rst
浏览文件 @
10ad0b50
...
@@ -270,4 +270,3 @@ Go语言中的函数,在你定义的时候,就规定了此函数
...
@@ -270,4 +270,3 @@ Go语言中的函数,在你定义的时候,就规定了此函数
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c04/c04_02.rst
浏览文件 @
10ad0b50
...
@@ -146,4 +146,3 @@ main 的地位相当于主线程,当 main
...
@@ -146,4 +146,3 @@ main 的地位相当于主线程,当 main
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c04/c04_03.rst
浏览文件 @
10ad0b50
...
@@ -206,7 +206,7 @@ int 等等)
...
@@ -206,7 +206,7 @@ int 等等)
type
Sender
=
chan
<-
int
type
Sender
=
chan
<-
int
sender
:=
make
(
Sender
)
sender
:=
make
(
Sender
)
代码是没问题,但是你要明白信道的意义是什么?
(
\
**
以下是我个人见解
**
代码是没问题,但是你要明白信道的意义是什么?
(**
以下是我个人见解
**
信道本身就是为了传输数据而存在的,如果只有接收者或者只有发送者,那信道就变成了只入不出或者只出不入了吗,没什么用。所以只读信道和只写信道,唇亡齿寒,缺一不可。
信道本身就是为了传输数据而存在的,如果只有接收者或者只有发送者,那信道就变成了只入不出或者只出不入了吗,没什么用。所以只读信道和只写信道,唇亡齿寒,缺一不可。
...
@@ -332,4 +332,3 @@ range关键字,在range时,要确保信道是处于关闭状态,否则循
...
@@ -332,4 +332,3 @@ range关键字,在range时,要确保信道是处于关闭状态,否则循
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c04/c04_04.rst
浏览文件 @
10ad0b50
...
@@ -179,4 +179,3 @@ range 信道已经关闭,无需等待就行。
...
@@ -179,4 +179,3 @@ range 信道已经关闭,无需等待就行。
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c04/c04_05.rst
浏览文件 @
10ad0b50
...
@@ -117,4 +117,3 @@ sync.WaitGroup。。
...
@@ -117,4 +117,3 @@ sync.WaitGroup。。
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c04/c04_06.rst
浏览文件 @
10ad0b50
...
@@ -232,4 +232,3 @@ defer。
...
@@ -232,4 +232,3 @@ defer。
|
image0
|
|
image0
|
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
..
|
image0
|
image
::
http
://
image
.
python
-
online
.
cn
/
image
-
20200320125724880.
png
source/c04/c04_08.rst
浏览文件 @
10ad0b50
...
@@ -187,4 +187,3 @@ https://studygolang.com/articles/13344
...
@@ -187,4 +187,3 @@ https://studygolang.com/articles/13344
|image0|
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
source/c05/c05_01.rst
浏览文件 @
10ad0b50
...
@@ -209,4 +209,3 @@
...
@@ -209,4 +209,3 @@
..
|
image0
|
image
::
/
Users
/
MING
/
Library
/
Application
%
20
Support
/
typora
-
user
-
images
/
image
-
20200324211928616.
png
..
|
image0
|
image
::
/
Users
/
MING
/
Library
/
Application
%
20
Support
/
typora
-
user
-
images
/
image
-
20200324211928616.
png
..
|
image1
|
image
::
/
Users
/
MING
/
Library
/
Application
%
20
Support
/
typora
-
user
-
images
/
image
-
20200324205302201.
png
..
|
image1
|
image
::
/
Users
/
MING
/
Library
/
Application
%
20
Support
/
typora
-
user
-
images
/
image
-
20200324205302201.
png
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录