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语 ...
随机推荐
- 【LOJ#6281】数列分块5
题目大意:维护一个有 N 个数组成的序列,支持查询区间元素和.区间元素向下取整的开方操作. 题解:由于序列中维护的数最大不超过整数的范围,而对于整数范围内的数来说,一个数在开方 5 次及以上时,结果不 ...
- linux下设置默认路径
查看文件: vim ~/.bash_profile 在bash_profile文件下以编辑模式插入以下代码:其中,/xxx/myname即为要设置的默认路径 SYSTEM=`uname -s` cas ...
- Log4j 2X 日志文件路径问题
关于路径问题网上说啥的都有,但是也不能说人家错,只能说不适合你这个. 一开始,我用的 ${webapp.root} <RollingFile name="rollingFileSy ...
- mysql名词解释
什么是QPS? 单位时间内所处理的事务数 什么是TPS? 单位时间内所处理的查询数 响应时间 并发量 同时处理的查询请求的数量 什么是吞吐量?
- JS 将字符串数组用 | 或其他符号分割
var arr = ["吕超","赵云","典韦","关羽","马超","张飞" ...
- A + B,末k位不相同
题目描述 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1. 输入描述: 测试输入包含若干测试用例,每个测试用例占一行,格式为& ...
- Unity3d跨平台原理
知乎的一个提问:unity3d跨平台原理 一些资料: IL IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe ...
- [C++]Linux之间隔时间内循环执行指定程序
#include<time.h> #include<unistd.h>//usleep(num) #include<stdio.h> #include<std ...
- d 属性: 赋予字段执行动作的能力
1.对只读属性误解 property AppSetting: ISuperobject read fAppSetting;当看到 AppInfo.AppSetting.D['lastLat'] := ...
- Android测试技能树
Android 基础知识 Android 的体系结构 apk 的组成结构 adb 命令的使用 Android 的四大组件 Activity 的生命周期 … 测试/开发环境的准备 JDK 安装 SDK ...