類型別名和自定義類型區別
自定義類型
//自定義類型是定義了一個全新的類型 //將MyInt定義為int類型 type MyInt int
類型別名
//類型別名規定:TypeAlias只是Type的別名,本質上TypeAlias與Type是同一個類型。 type TypeAlias = Type type byte = uint8 type rune = int32
區別
類型別名與類型定義表面上看只有一個等號的差異
//類型定義 type NewInt int //類型別名 type MyInt = int func main() { var a NewInt var b MyInt fmt.Printf("type of a:%T\n", a) //type of a:main.NewInt fmt.Printf("type of b:%T\n", b) //type of b:int }
//區別
//結果顯示a的類型是main.NewInt
,表示main包下定義的NewInt
類型。b的類型是int
。MyInt
類型只會在代碼中存在,編譯完成時並不會有MyInt
類型。