// 实现者
package fake
type Retriever struct{
Contents string
}
func (r *Retriever) Get(url string) string {
return r.Contents
}
func (r *Retriever) Post(url string,
form map[string]string) string {
r.Contents = form["contents"]
return "ok"
}
// 使用者
// 接口一
type Retriever interface {
Get(url string) string
}
// 接口二
type Poster interface {
Post(url string,
form map[string]string) string
}
// 将上面的两个接口进行组合
type RetrieverPoster interface {
Retriever
Poster
}
// 使用组合接口
func session(s RetrieverPoster) string {
s.Post("https://www.hyperledger.org/", map[string]string{
"contents": "hyperledger fabric",
})
return s.Get("https://www.hyperledger.org/")
}