当前位置:   article > 正文

Go 知识点(05)— 类型别名与类型定义_go 别名

go 别名

1. 类型别名

类型别名需要在别名和原类型之间加上赋值符号 = ,使用类型别名定义的类型与原类型等价,Go 语言内建的基本类型中就存在两个别名类型。

  • byteuint8 的别名类型;
  • runeint32 的别名类型;

类型别名定义形式如下:

type MyString = string
  • 1

上面代码表MyStringstring 类型的别名类型。也就是说别名类型和源类型表示的是同一个目标,就譬如每个人的学名和乳名一样,都表示同一个人。

定义 string 类型的别名,示例代码:

func main() {
	type MyString = string
	str := "hello"
	a := MyString(str)
	b := MyString("A" + str)
	fmt.Printf("str type is %T\n", str)
	fmt.Printf("a type is %T\n", a)
	fmt.Printf("a == str is %t\n", a == str)
	fmt.Printf("b > a is %t\n", b > a)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

输出结果

str type is string
a type is string
a == str is true
b > a is false
  • 1
  • 2
  • 3
  • 4

定义 []string 类型的别名,示例代码:

func main() {
	type MyString = string
	strs := []string{"aa", "bb", "cc"}
	a := []MyString(strs)

	fmt.Printf("strs type is %T\n", strs)
	fmt.Printf("a type is %T\n", a)
	fmt.Printf("a == nil is %t\n", a == nil)
	fmt.Printf("strs == nil is %t\n", strs != nil)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行结果为:

strs type is []string
a type is []string
a == nil is false
strs == nil is true
  • 1
  • 2
  • 3
  • 4

从上面结果可以得出以下结论:

  • 别名类型与源类型是完全相同的;
  • 别名类型与源类型可以在源类型支持的条件下进行相等判断、比较判断、与 nil 是否相等判断等;

2. 类型定义

类型定义是定义一种新的类型,它与源类型是不一样的。看下面代码:


func main() {
	type MyString string
	str := "hello"
	a := MyString(str)
	b := MyString("A" + str)
	fmt.Printf("str type is %T\n", str)
	fmt.Printf("a type is %T\n", a)
	fmt.Printf("a value is %#v\n", a)
	fmt.Printf("b value is %#v\n", b)
	// fmt.Printf("a == str is %t\n", a == str)
	fmt.Printf("b > a is %t\n", b > a)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出结果为:

str type is string
a type is main.MyString
a value is "hello"
b value is "Ahello"
b > a is false
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到 MyString 类型为 main.MyString 而原有的 str 类型为 string,两者是不同的类型,如果使用下面的判断相等语句

fmt.Printf("a == str is %t\n", a == str)
  • 1

会有编译错误提示

invalid operation: a == str (mismatched types MyString and string)
  • 1

说明类型不匹配。

下面代码

func main() {
	type MyString string

	strs := []string{"E", "F", "G"}
	myStrs := []MyString(strs)
	fmt.Println(myStrs)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

编译报错提示

cannot convert strs (type []string) to type []MyString
  • 1

类型定义与别名

对于这里的类型再定义来说,string 可以被称为 MyString2 的潜在类型。潜在类型的含义是,某个类型在本质上是哪个类型。潜在类型相同的不同类型的值之间是可以进行类型转换的。

因此,MyString2 类型的值与 string 类型的值可以使用类型转换表达式进行互转。但对于集合类的类型[]MyString2[]string 来说这样做却是不合法的,因为 []MyString2[]string 的潜在类型不同,分别是 []MyString2[]string

另外,即使两个不同类型的潜在类型相同,它们的值之间也不能进行判等或比较,它们的变量之间也不能赋值。

func main() {
	type MyString1 = string
	type MyString2 string
	str := "BCD"
	myStr1 := MyString1(str)
	myStr2 := MyString2(str)
	myStr1 = MyString1(myStr2)
	myStr2 = MyString2(myStr1)

	myStr1 = str

	myStr2 = str
	// cannot use str (type string) as type MyString2 in assignment

	myStr1 = myStr2
	// cannot use myStr2 (type MyString2) as type string in assignment

	myStr2 = myStr1
	// cannot use myStr1 (type string) as type MyString2 in assignment
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

参考:
https://time.geekbang.org/column/article/13601

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/42943
推荐阅读
相关标签
  

闽ICP备14008679号