2010-05-15 64 views
0

下一個實現接口方法的方法是否正確? (getKeygetData接口的方法

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 
} 

回答

4

在去,你不需要直接說你實現一個接口,如果一個類型都有一個接口所需的一切,可以通過該接口使用。所以你不需要在type location struct裏面說reader

在這裏看到:http://golang.org/doc/effective_go.html#interfaces_and_types

+0

事實上,不把'reader'實際上只是給''location'類型添加一個匿名成員,佔用空間不做任何有用的事情? – matthias 2012-08-31 17:20:09

1

你已經基本上完成了它了。只要您給位置的getKey和getData方法有效的實體,*位置就會實現閱讀器界面。沒有必要再做任何事了。