MDGSF Software Engineer

[GO] type定义新类型和别名

2018-04-16
GO

package main

import "fmt"

type newInt int  // This is a new type.
type myInt = int // This is just a alias name.

func main() {
    var a newInt
    var b myInt
    var c int

    fmt.Printf("a = %v, type = %T\n", a, a)
    fmt.Printf("b = %v, type = %T\n", b, b)
    fmt.Printf("c = %v, type = %T\n", c, c)

    // c = a  // cannot use a (type newInt) as type int in assignment
    c = int(a) //valid
    c = b      //valid
}
a = 0, type = main.newInt
b = 0, type = int
c = 0, type = int

weixingongzhonghao

Comments

Content