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在它的类型声明中 应用的 类型的底层类型。

好,感觉看懂这第二句话了!“在它的类型声明中”,不就是本文前面讲的 类型别名 和 新类型定义 吗?

怎么找到一个类型的底层类型呢?请参考下面的链接(在昨天有使用过):

golang如何获取变量的类型:反射,类型断言

好勒,就这么多!感觉自己又进步了!稍后学习下上面的 Expression -> Composite literals

Go语言规格说明书 之 类型声明(Type declarations)的更多相关文章

  1. Go语言规格说明书 之 类型(Types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  2. Go语言规格说明书 之 变量声明(Variable/Short variable declarations)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  3. drools的类型声明(Type declarations)

    一.背景 在我们编写drl规则的时候,有些时候需要自己声明一些类,用于辅助之后的规则运行,如果需要用到的类还需要在java中预先声明出来,这样就不灵活了,那么是否可以在drl文件中声明一个类呢?可以使 ...

  4. Go语言规格说明书 之 通道 发送语句(send) 和 接收操作符(receive)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  5. Go语言规格说明书 之 通道类型(Channel types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  6. Go语言规格说明书 之 接口类型(Interface types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的  ...

  7. Go语言规格说明书 之 结构体类型(Struct types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  8. Go语言规格说明书 之 内建函数(Built-in functions)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  9. Go语言规格说明书 之 词汇元素(Lexical elements)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

随机推荐

  1. [luogu2114][起床困难综合症]

    luogu2114 思路 因为位运算对于每一位是独立的,所以对每一位都对这n个数进行操作,然后观察最后得出的是1还是0.并且保证每一位拼起来之后要比m小. 代码 #include<cstdio& ...

  2. 2156: 中南大学2018年ACM暑期集训前期训练题集(入门题) D: 机器人的指令

    不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! ...

  3. Mac下Vim编辑快捷键小结(移动光标)

    Mac下Vim编辑快捷键小结(移动光标) 1.移动到行尾"$",移动到行首"0"(数字),移动到行首第一个字符处"^" 2.移动到段首&qu ...

  4. asp.net上传图片文件自动修改图片大小代码

    #region 图片缩放 /// <summary> /// 图片缩放 /// </summary> /// <param name="savePath&quo ...

  5. SQL Server 日期函数大全

    一.统计语句 1.--统计当前[>当天00点以后的数据] SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT( ...

  6. 多目标遗传算法 ------ NSGA-II (部分源码解析)父、子种群合并 merge.c

    /* Routine for mergeing two populations */ # include <stdio.h> # include <stdlib.h> # in ...

  7. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  8. Linux 命令详解(八)Systemd 入门教程:实战篇

    Systemd 入门教程:实战篇 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

  9. Pool多进程示例

    利用Pool类多进程实现批量主机管理 #!/usr/bin/python # -*- coding: UTF-8 -*- # Author: standby # Time: 2017-03-02 # ...

  10. Lucene Query In Kibana

    1. Terms 一个查询由词条与操作组成.词条可以是单词,或者短语. hello #单独项 "hello pzdn" #双引号引起来短语 2. Field Lucene 支持字段 ...