下一个实现附加到接口(interface)的方法的方法是否正确? (getKey
, getData
)
type reader interface {
getKey(ver uint) string
getData() string
}
type location struct {
reader
fileLocation string
err os.Error
}
func (self *location) getKey(ver uint) string {...}
func (self *location) getData() string {...}
func NewReader(fileLocation string) *location {
_location := new(location)
_location.fileLocation = fileLocation
return _location
}
最佳答案
在 Go 中,你不需要明确地说你正在实现一个接口(interface)——如果一个类型具有接口(interface)所需的一切,它就可以通过该接口(interface)使用。所以你不需要在 type location struct
中说 reader
。
参见此处:http://golang.org/doc/effective_go.html#interfaces_and_types
关于interface - 接口(interface)的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2841901/