golang基础 - 自定义类型和类型别名(type)

自定义类型

Go语言通过type关键字定义自定义类型。自定义类型是全新的类型。
示例:

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

func main() {
	var a newInt
	a = 100
	fmt.Println(a)        // 100
	fmt.Printf("%T\n", a) // main.newInt
}

上例中的newInt是具有int特性的新类型。可以看到变量a的类型是main.newInt,这表示main包下定义的newInt类型。

类型别名

语法格式:type 别名 = Type
示例:

type tempString = string

func main() {
	var s tempString
	s = "我是s"
	fmt.Println(s)        // 我是s
	fmt.Printf("%T\n", s) // string
}

例中,tempStringstring的别名,其本质上与string是同一个类型。类型别名只会在代码中存在,编译完成后不会有如tempString一样的类型别名。所以变量s的类型是string
字符类型中的byterune就是类型别名:

type byte = uint8
type rune = int32

类型别名这个功能非常有用,鉴于go中有些类型写起来非常繁琐,比如json相关的操作中,经常用到map[string]interface {}这种类型,写起来是不是很繁琐,没关系,给它起个简单的别名!这样用起来爽多了。

type strMap2Any = map[string]interface {}

 

posted @ 2021-11-09 11:48  梁天  阅读(1563)  评论(0编辑  收藏  举报