Go中Type关键字有哪几种用法
在Go语言中,type关键字有以下几种用法:
type Ttype int,这里定义了一个名为Ttype的类型,它是一个int类型的别名。type Person struct {Name string; Age int},这里定义了一个名为Person的结构体,它包括Name和Age两个字段。type Writer interface {Write(p []byte) (n int, err error)},这里定义了一个名为Writer的接口,它包括一个Write方法。type Add func(a, b int) int,这里定义了一个名为Add的函数类型,它接收两个int类型的参数,并返回一个int类型的结果。func (p *Person) SayHello() {fmt.Println("Hello", p.Name)},这里给Person类型定义了一个SayHello方法。value, ok := x.(T),这里用于判断x是否是为类型T的实例,并返回其对应的值和布尔值结果。y := T(x),这里将x转换为类型T,其中T可以是内置类型或自定义类型。
TOP