Golang-type关键字

type 在 Golang 中非常常见的关键字,一般的用法就是定义结构,接口等。 但是 type 还有很多其它的用法。

定义结构体

type Person struct {
    name string
    age string
}

type Mutex struct {}

type otherMutex Mutex

定义接口

type Person interface {
    ShowName(s string)
}

定义类型

type Myint int
type (m Myint) showValue() {
    fmt.Println("show int", m)
}

别名定义

定义和原来一样的类型,就是一个alias

定义函数类型

有一个关于 type 示例

参考

*type 关键字使用

Last updated

Was this helpful?