Methods
Methods are functions that have a receiver (see ).You can define methods on any type (except on non-local types, this includesbuilt-in types: the type int
can not have methods).You can however make a new integer type with its own methods. For example:
type Foo int
func (self Foo) Emit() {
fmt.Printf("%v", self)
}
type Emitter interface {
Emit()
}
Doing this on non-local (types defined in other packages) types yields an error“cannot define new methods on non-local type int”.
当前内容版权归 Miek Gieben 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Miek Gieben .