2010-05-15 3 views
0

다음 방법으로 인터페이스에 연결된 메서드를 구현하는 것이 맞습니까? 유형은 인터페이스에 필요한 모든 것을 갖추고 명시 적으로 당신이 구현되어 있다고 할 필요가 없습니다 이동에서 (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 
} 

답변

4

경우 인터페이스는-, 그것은 그 인터페이스를 통해 사용할 수 있습니다. 따라서 type location struct 안에 reader을 말할 필요는 없습니다.

는 여기를 참조하십시오 : http://golang.org/doc/effective_go.html#interfaces_and_types

+0

사실 'reader'는 실제로 'location' 유형에 익명의 멤버를 추가하지 않고 아무것도 유용하지 않은 공간을 차지하지 않으십니까? – matthias

1

당신은 기본적으로 이미 완료했습니다. 위치의 getKey 및 getData 메소드에 유효한 본문을 지정하자마자 * location은 판독기 인터페이스를 구현합니다. 더 이상 아무것도 할 필요가 없습니다.

관련 문제