Go语言规格说明书 之 类型声明(Type declarations)
go version go1.11 windows/amd64
本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语言的 类型声明(Type declarations)。
类型声明 即 绑定一个标识符(新类型名称) 到 一个类型。
有两种形式:类型别名声明(alias declarations)、新类型定义(type definitions)。
类型别名声明
很简单:在类型别名和类型之间使用等号(=)。官文示例:
type (
nodeList = []*Node // nodeList and []*Node are identical types
Polar = polar // Polar and polar denote identical types
)
注意,类型别名 也是一个标识符,也是有其 作用域(scope)的。
新类型定义
这个复杂一些,还好,自己理解了90%了。
创建一个新的类型,这个类型 和 给定的已存在的(旧)类型 有相同的底层类型和操作,并用一个新的 标识符 来表示这个新类型。
这样的 新类型 也叫做 自定义类型(defined type)——解决了昨天的困惑,新类型和其它类型是不同的,即便是用来创造它的旧类型。
官文示例:
新旧类型之间 没有等号(=),空格分隔;
通过示例,知道了之前使用的 type+struct/interface原来是在创建新的类型;
type (
Point struct{ x, y float64 } // Point and struct{ x, y float64 } are different types
polar Point // polar and Point denote different types
) type TreeNode struct {
left, right *TreeNode
value *Comparable
} type Block interface {
BlockSize() int
Encrypt(src, dst []byte)
Decrypt(src, dst []byte)
}
进阶1:
新类型 可以拥有自己的方法,但是,新类型 不会继承 任何旧类型的方法, 就是说,想要拥有方法,需要自行定义。
但是(但是来了),接口类型、复合类型(a composite type)的元素 的方法保持不变。
官文示例(用到了上面示例中的Block类型):
// A Mutex is a data type with two methods, Lock and Unlock.
type Mutex struct { /* Mutex fields */ }
func (m *Mutex) Lock() { /* Lock implementation */ }
func (m *Mutex) Unlock() { /* Unlock implementation */ }
// 类型Mutex有两方法:Lock,Unlock
// NewMutex has the same composition as Mutex but its method set is empty.
type NewMutex Mutex
// 新类型NewMutex的方法集是空的
// The method set of PtrMutex's underlying type *Mutex remains unchanged,
// but the method set of PtrMutex is empty.
type PtrMutex *Mutex
// 新类型PtrMutex的方法集也是空的,,这个指针看来不是复合类型啊,,
// The method set of *PrintableMutex contains the methods
// Lock and Unlock bound to its embedded field Mutex.
type PrintableMutex struct {
Mutex
}
// 疑问:为何是*PrintableMutex?这个就是复合类型吗?到底什么是复合类型?有哪些? // MyBlock is an interface type that has the same method set as Block.
type MyBlock Block
// 这里的Block是 指针类型的,前一个官文示例中定义了
进阶2:
新类型定义可以用于定义不同的 布尔类型、数字类型、字符串类型,然后给新类型添加方法。
下面是官文示例:
type TimeZone int const (
EST TimeZone = -(5 + iota)
CST
MST
PST
) func (tz TimeZone) String() string {
return fmt.Sprintf("GMT%+dh", tz)
}
测试代码:
package main import (
"fmt"
) type TimeZone int const (
EST TimeZone = -(5 + iota)
CST
MST
PST
) func (tz TimeZone) String() string {
return fmt.Sprintf("GMT%+dh", tz)
} func main() {
var x TimeZone = 1
fmt.Println(x.String())
x = 123
fmt.Println(x.String())
x = -999123
fmt.Println(x.String()) fmt.Println(EST, CST, MST, PST)
}
测试 type TimeZone int
测试结果:
GMT+1h
GMT+123h
GMT-999123h
GMT-5h GMT-6h GMT-7h GMT-8h
上面的示例有两个知识点自己还需要去dig:const声明、iota的使用。
给 新类型 TimeZone 添加了方法String(),结果,使用fmt.Sprintf()打印这几个常量时就出现了其方法String()返回的字符串了。
这个String()方法一定有特别的用途的!
对了,还有 复合类型!这也是个重难点!在官文中找到了说明:Expression -> Composite literals。
对了,还有 底层类型!昨天的官文是这么介绍的:
Each type T has an underlying type:
If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself.
Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration.
翻译:
如果 类型T 是预定义的 布尔、数字、字符串 类型,或者 类型字面量(不理解?),那么,其相应的底层类型是 T 本身。
否则,类型T 的底层类型是 类型T在它的类型声明中 应用的 类型的底层类型。
好,感觉看懂这第二句话了!“在它的类型声明中”,不就是本文前面讲的 类型别名 和 新类型定义 吗?
怎么找到一个类型的底层类型呢?请参考下面的链接(在昨天有使用过):
好勒,就这么多!感觉自己又进步了!稍后学习下上面的 Expression -> Composite literals。
Go语言规格说明书 之 类型声明(Type declarations)的更多相关文章
- Go语言规格说明书 之 类型(Types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- Go语言规格说明书 之 变量声明(Variable/Short variable declarations)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- drools的类型声明(Type declarations)
一.背景 在我们编写drl规则的时候,有些时候需要自己声明一些类,用于辅助之后的规则运行,如果需要用到的类还需要在java中预先声明出来,这样就不灵活了,那么是否可以在drl文件中声明一个类呢?可以使 ...
- Go语言规格说明书 之 通道 发送语句(send) 和 接收操作符(receive)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Go语言规格说明书 之 通道类型(Channel types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Go语言规格说明书 之 接口类型(Interface types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Go语言规格说明书 之 结构体类型(Struct types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Go语言规格说明书 之 内建函数(Built-in functions)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- Go语言规格说明书 之 词汇元素(Lexical elements)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
随机推荐
- 许仙章鱼TV
http://v.youku.com/v_show/id_XMTY3NTYwNTE4MA==.html?from=s1.8-1-1.2&spm=a2h0k.8191407.0.0 201608 ...
- 【题解】亚瑟王 HNOI 2015 BZOJ 4008 概率 期望 动态规划
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4008 一道不简单的概率和期望dp题 根据期望的线性性质,容易想到,可以算出每张卡的期望伤害, ...
- ngrep 比 tcpdump 更方便查看的抓包显示工具
ngrep 是grep(在文本中搜索字符串的工具)的网络版,他力求更多的grep特征,用于搜寻指定的数据包 一: ngrep的安装 CentOS6.2 64位 wget http://nchc.dl. ...
- Ruler.java
/****************************************************************************** * Compilation: javac ...
- 算法入门及其C++实现
https://github.com/yuwei67/Play-with-Algorithms (nlogn)为最优排序算法 选择排序 整个数组中,先选出最小元素的位置,将该位置与当前的第一位交换:然 ...
- Jenkins插件获取git分支的方法
1.旧版本的Jenkins可以使用Dynamic Choice Parameter插件: 使用方法: Jenkins--->dev-h5-server--->配置--->参数化构建过 ...
- 紧急通知:Onion勒索病毒正在大范围传播!已有大量学生中招!(转)
在5月12日晚上20点左右,全国各地的高校学生纷纷反映,自己的电脑遭到病毒的攻击,文档被加密,壁纸遭到篡改,并且在桌面上出现窗口,强制学生支付等价300美元的比特币到攻击者账户上.我们的一位成员和其多 ...
- window netsh interface portproxy 配置转发
系统版本 windows server2016 datacenter 1.配置443.80端口转发到其他服务器的443.80上 netsh interface portproxy add v4tov4 ...
- Spark记录-SparkSQL相关学习
$spark-sql --help 查看帮助命令 $设置任务个数,在这里修改为20个 spark-sql>SET spark.sql.shuffle.partitions=20; $选择数据 ...
- ibatis (mybatis) for循环拼接语句【转】
使用 , 拼接 查询条件dto public class queryCondition{ private String[] stuIds; private String name;} 查询sqlMap ...