开发者

Methods of an interface

Would be correct the next way to implement the methods attached to an interface? (getKey, getData)

type reader interface {
    getKey(ver uint) string
    getData() string
}

type locatio开发者_C百科n 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
}


In Go you don't need to explicitly say that you are implementing an interface—if a type has everything required by an interface, it can be used via that interface. So you don't need to say reader inside the type location struct.

See here: http://golang.org/doc/effective_go.html#interfaces_and_types


You've basically done it already. As soon as you give location's getKey and getData methods valid bodies, *location will implement the reader interface. There's no need to do anything more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜