相关文章推荐
不拘小节的瀑布  ·  sql中where ...·  2 年前    · 
鼻子大的汤圆  ·  c++ 解析yaml文件 - ...·  2 年前    · 

上例中的 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 {}这种类型,写起来是不是很繁琐,没关系,给它起个简单的别名!这样用起来爽多了。