热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

55常用系统接口

main.gopackagemainimport(fmtretrievermockretrieverrealtime)consturlhttp:www.imo

main.go

package mainimport ("fmt""retriever/mock""retriever/real""time"
)const url = "http://www.imooc.com"
// 下载接口
type Retriever interface {Get(url string) string
}func download(r Retriever) string {return r.Get(url)
}// 上传接口
type Poster interface {Post(utl string, form map[string]string) string
}func post(poster Poster) {poster.Post(url,map[string]string {"name": "ccmouse","course": "golang",})
}// 可以有多个小接口组合
type RetrieverPoster interface {RetrieverPoster
}
func session(s RetrieverPoster) string {s.Post(url, map[string]string {"contents": "another faked imooc.com",})return s.Get(url)
}func main() {var r Retriever// mock/mockretriever.go// mock.Retriever现在是RetrieverPoster接口fmt.Println("-----------测试mock.Retriever接口----------------")r = &mock.Retriever{Contents:"this is a fake imooc.com"}inspect(r)// fmt.Println(download(r))//Type assertionmockRetriever := r.(*mock.Retriever)fmt.Println("Type assertion: "+mockRetriever.Contents)fmt.Println("\n------------测试real.RealRetriever接口---------------")// real/retriever.gor = &real.RealRetriever{UserAgent: "Mozilla/5.0",TimeOut: time.Minute,}inspect(r)// fmt.Println(download(r))//Type assertionrealRetriever := r.(*real.RealRetriever)fmt.Println("Type assertion: "+realRetriever.UserAgent)// 判断接口的类型if mockRetriever,ok := r.(*mock.Retriever); ok {fmt.Println("Type assertion: "+mockRetriever.Contents)} else {fmt.Println("not mock a retriever")}fmt.Println("\n----------测试RetrieverPoster接口-----------------")fmt.Println(session(mockRetriever))
}// 如何判断接口的类型
func inspect(r Retriever) {// %T查看类型 %v查看数值fmt.Printf(">类型:%T 数值:%v\n", r, r)fmt.Printf(">Type switch:")switch v := r.(type) {case *mock.Retriever:fmt.Println("Contents: "+v.Contents)case *real.RealRetriever:fmt.Println("UserAgent: "+v.UserAgent)}//mock.Retriever {this is a fake imooc.com}//Contents: this is a fake imooc.com//*real.RealRetriever &{Mozilla/5.0 1m0s}//UserAgent: Mozilla/5.0
}

mock/mockretriever.go

package mockimport ("fmt"
)// mock.Retriever现在是RetrieverPoster接口
type Retriever struct {Contents string
}// 系统接口
// fmt.Printf("%v", )
func (r *Retriever) String() string {return fmt.Sprintf("Retriever: {Contents=%s}", r.Contents)
}//func (r *Retriever)Get(url string) string {
func (r Retriever)Get(url string) string {return r.Contents
}// 这里需要是r *Retriever,否则原对象的r.Contents数值不会改变
func (r *Retriever)Post(url string,form map[string]string) string {r.Contents = form["contents"]return "ok"
}

real/retriever.go

package realimport ("time""net/http""net/http/httputil"
)type RealRetriever struct {UserAgent stringTimeOut time.Duration
}func (r *RealRetriever) Get(url string) string {resp, err := http.Get(url)if err != nil {panic(err)}result, err := httputil.DumpResponse(resp, true)resp.Body.Close()if err != nil {panic(err)}return string(result)
}

 


推荐阅读
author-avatar
木扎尔特2502918527
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有