Go: 5-1 接口

duck typing
pythn中的duck typing
def download(retriever):
    return retriever.get("www.downloadurl.com")
c++ 中的duck typing
template<class R>
string download(const R& retriever){
    return retriever.get("www.downloadur..com")
}
java 中的类似代码
<R extends Retriever>
String download(R r){
    return r.get("www.downloadurl.com")
}
go 语言的duck typing
接口的定义
接口遍历里有什么东西
查看接口变量

	//Type assertion
	if mockRetriver, ok := r.(*mock.Retriever); ok {
		fmt.Println(mockRetriver.Contents)
	} else {
		fmt.Println("not mock retriver")

	}
	
    fmt.Printf("%T %v\n", r, r)
	fmt.Println(r)
	switch v := r.(type) {
	case mock.Retriever:
		fmt.Println("contents:", v.Contents)
	case *real.Retriever:
		fmt.Println("UserAgent:", v.UserAgent)

	}	

接口的组合
//interface 1
type Retriever interface {
	Get(url string) string
}
//interface 2
type Poster interface {
	Post(url string, form map[string]string) string
}

//interface  combination
type RetrieverPoster interface {
	Retriever
	Poster
}

//go lib  readWriter

// ReadWriter is the interface that groups the basic Read and Write methods.
type ReadWriter interface {
	Reader
	Writer
}

// ReadCloser is the interface that groups the basic Read and Close methods.
type ReadCloser interface {
	Reader
	Closer
}

// WriteCloser is the interface that groups the basic Write and Close methods.
type WriteCloser interface {
	Writer
	Closer
}

// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
type ReadWriteCloser interface {
	Reader
	Writer
	Closer
}

常用系统接口