Go语言type关键字(类型别名)

2020-01-30  本文已影响0人  码二哥

参考:
http://c.biancheng.net/view/25.html

关键点

1、区分类型定义类型别名

package main

import (
    "fmt"
)

// 将NewInt定义为int类型
type NewInt int

// 将int取一个别名叫IntAlias
type IntAlias = int

func main() {

    // 将a声明为NewInt类型
    var a NewInt
    // 查看a的类型名
    fmt.Printf("a type: %T\n", a)

    // 将a2声明为IntAlias类型
    var a2 IntAlias
    // 查看a2的类型名
    fmt.Printf("a2 type: %T\n", a2)
}
image

代码说明如下:

结果显示 :
a 的类型是 main.NewInt,表示 main 包下定义的 NewInt 类型,
a2 类型是 int,IntAlias 类型只会在代码中存在,编译完成时,不会IntAlias 类型

2、非本地类型不能定义方法

能够随意地为各种类型起名字,是否意味着可以在自己包里为这些类型任意添加方法呢?
参见下面的代码演示:

package main

import (
    "time"
)

// 定义time.Duration的别名为MyDuration
type MyDuration = time.Duration

// 为MyDuration添加一个函数
func (m MyDuration) EasySet(a string) {

}

func main() {

}
image

代码说明如下:

编译器提示:
不能在一个非本地类型 time.Duration 上定义新方法,

非本地类型指的就是 time.Duration 不是在 main 包中定义的,而是在 time 包中定义的,与 main 包不在同一个包中,因此不能为不在一个包中的类型定义方法。

解决这个问题有下面两种方法:

3、在结构体成员嵌入时使用别名

类型别名作为结构体嵌入成员时会发生什么情况呢?
请参考下面的代码。

package main

import (
    "fmt"
    "reflect"
)

// 定义商标结构
type Brand struct {
}

// 为商标结构添加Show()方法
func (t Brand) Show() {
}

// 为Brand定义一个别名FakeBrand
type FakeBrand = Brand

// 定义车辆结构
type Vehicle struct {

    // 嵌入两个结构
    FakeBrand
    Brand
}

func main() {

    // 声明变量a为车辆类型
    var a Vehicle
   
    // 指定调用FakeBrand的Show
    a.FakeBrand.Show()

    // 取a的类型反射对象
    ta := reflect.TypeOf(a)

    // 遍历a的所有成员
    for i := 0; i < ta.NumField(); i++ {

        // a的成员信息
        f := ta.Field(i)

        // 打印成员的字段名和类型
        fmt.Printf("FieldName: %v, FieldType: %v\n", f.Name, f.Type.
            Name())
    }
}

代码输出如下:

FieldName: FakeBrand, FieldType: Brand
FieldName: Brand, FieldType: Brand

代码说明如下:

第 9 行,定义商标结构。
第 13 行,为商标结构添加 Show() 方法。
第 17 行,为 Brand 定义一个别名 FakeBrand。
第 20~25 行,定义车辆结构 Vehicle,嵌入 FakeBrand 和 Brand 结构。
第 30 行,将 Vechicle 实例化为 a。
第 33 行,显式调用 Vehicle 中 FakeBrand 的 Show() 方法。
第 36 行,使用反射取变量 a 的反射类型对象,以查看其成员类型。
第 39~42 行,遍历 a 的结构体成员。
第 45 行,打印 Vehicle 类型所有成员的信息。

这个例子中,FakeBrand 是 Brand 的一个别名,在 Vehicle 中嵌入 FakeBrand 和 Brand 并不意味着嵌入两个 Brand,FakeBrand 的类型会以名字的方式保留在 Vehicle 的成员中。

如果尝试将第 33 行改为:

a.Show()

编译器将发生报错:

ambiguous selector a.Show

在调用 Show() 方法时,因为两个类型都有 Show() 方法,会发生歧义,

证明 FakeBrand 的本质确实是 Brand类型。

4、新产生的类型 是否 继承了 基础类型的方法呢?

image
上一篇 下一篇

猜你喜欢

热点阅读