2014-11-11 4 views
2

나는 골란의 유형과 인터페이스를 감싸하려고 노력하고 있지만 그렇게하기에는 조금 어려움을 겪고 있습니다. 어쨌든, 보시는 공통 패턴은 func Whatever() (thing string, err error)입니다. 나는 그 모든 것들이 어떻게 작동하는지 알지만, 내가 혼란스러워하는 한가지 이유는 return "thing", nil이다. 내가보고하고 특정 인스턴스는 여기 -Golang return nil

func (c *GorpController) Begin() revel.Result { 
    txn, err := Dbm.Begin() 
    if err != nil { 
     panic(err) 
    } 
    c.Txn = txn 
    return nil 
} 

revel.Result 그 때의 nil 만족 컴파일러를 반환하는 방법 난 그냥 궁금,

type Result interface { 
    Apply(req *Request, resp *Response) 
} 

어쨌든이 signature-과의 인터페이스 르벨에 . 내가 지적 할 수있는 자원이 있습니까?

답변

9

이것은 무 에러를 반환 유사하다 : 여기 nil "Why is my nil error value not equal to nil?"

Under the covers, interfaces are implemented as two elements, a type and a value.

The value, called the interface's dynamic value, is an arbitrary concrete value and the type is that of the value. For the int value 3 , an interface value contains, schematically, (int, 3).

An interface value is nil only if the inner value and type are both unset, (nil, nil). In particular, a nil interface will always hold a nil type.
If we store a pointer of type *int inside an interface value, the inner type will be *int regardless of the value of the pointer: (*int, nil).
Such an interface value will therefore be non-nil even when the pointer inside is nil .

인터페이스 revel.Result의 제로 값을 발생한다.

+1

완벽한 설명. 그렇게 해줘서 고마워. Go는 내가 배웠던 동시에 가장 쉽고 어려운 언어 인 언어 중 하나입니다. 인터페이스에 대한 자세한 내용은 –

+1

@BrandonHansen : http://stackoverflow.com/a/23148998/6309 – VonC