package main
import (
"fmt"
)
type A struct {
Face int
}
type Aa A // 自定义新类型Aa,没有基础类型A的方法
func (a A) f() {
fmt.Println("hi ", a.Face)
}
func main() {
var s A = A{ Face: 9 }
s.f()
var sa Aa = Aa{ Face: 9 }
sa.f()
}
编译错误信息:sa.f undefined (type Aa has no field or method f)
type IZ = int
package main
import (
"fmt"
)
type A struct {
Face int
}
type Aa=A // 类型别名
func (a A) f() {
fmt.Println("hi ", a.Face)
}
func main() {
var s A = A{Face: 9}
s.f()
var sa Aa = Aa{Face: 9}
sa.f()
}
程序输出:
hi 9
hi 9
type typeFunc func ( int, int) int
return varfunc
// Mutex 用两种方法,Lock and Unlock。
type Mutex struct { /* Mutex fields */ }
func (m *Mutex) Lock() { /* Lock implementation */ }
func (m *Mutex) Unlock() { /* Unlock implementation */ }
// NewMutex和 Mutex 一样的数据结构,但是其方法是空的。
type NewMutex Mutex
// PtrMutex 的方法也是空的
type PtrMutex *Mutex
// *PrintableMutex 拥有Lock and Unlock 方法
type PrintableMutex struct {
Mutex
}