Go语言规格说明书 之 类型声明(Type declarations) 您所在的位置:网站首页 语言类型的书 Go语言规格说明书 之 类型声明(Type declarations)

Go语言规格说明书 之 类型声明(Type declarations)

2024-06-19 01:03| 来源: 网络整理| 查看: 265

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。

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有