接口变量
接口的定义和实现
main
package mainimport (
"fmt"
"learngo/retriever/mock"
"learngo/retriever/test"
"time"
)type Retriever interface {
Get(url string) string
}func download(r Retriever) string {
return r.Get("https://blog.csdn.net/qq_43135259/article/details/123298792?spm=1001.2014.3001.5502")
}func main() {
r := mock.Retriever{"番茄炒蛋",time.Minute}
// HTTP/2.0 200 OK
fmt.Println(download(r))
rr:= test.Retriever{"this is fake contents"}
// this is fake contents
fmt.Println(download(rr))
}
mock.Retriever
package mockimport (
"net/http"
"net/http/httputil"
"time"
)type Retriever struct {
UserAgrent string
TimeOuttime.Duration
}func (r Retriever) Get(url string) string {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bytes, err := httputil.DumpResponse(resp, true)
if err != nil {
panic(err)
}
return string(bytes)
}
test.Retriever
package testtype Retriever struct {
Contents string
}func (r Retriever) Get(url string) string {
return r.Contents
}
接口变量内容
package mainimport (
"fmt"
"learngo/retriever/mock"
"learngo/retriever/test"
"time"
)type Retriever interface {
Get(url string) string
}func download(r Retriever) string {
return r.Get("https://blog.csdn.net/qq_43135259/article/details/123298792?spm=1001.2014.3001.5502")
}func main() {
r := mock.Retriever{"番茄炒蛋", time.Minute}
// HTTP/2.0 200 OK
//fmt.Println(download(r)) // mock.Retriever {番茄炒蛋 1m0s}
fmt.Printf("%T %v\n", r, r)
rr := test.Retriever{"this is fake contents"}
// this is fake contents
//fmt.Println(download(rr)) // test.Retriever {this is fake contents}
fmt.Printf("%T %v\n", rr, rr)
}
r和rr里面有一个类型,和自己真实的value通过指针访问
mock.Retriever
package mockimport (
"net/http"
"net/http/httputil"
"time"
)type Retriever struct {
UserAgrent string
TimeOuttime.Duration
}func (r *Retriever) Get(url string) string {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bytes, err := httputil.DumpResponse(resp, true)
if err != nil {
panic(err)
}
return string(bytes)
}
main
package mainimport (
"fmt"
"learngo/retriever/mock"
"learngo/retriever/test"
"time"
)type Retriever interface {
Get(url string) string
}func download(r Retriever) string {
return r.Get("https://blog.csdn.net/qq_43135259/article/details/123298792?spm=1001.2014.3001.5502")
}func main() {
r := &mock.Retriever{"番茄炒蛋", time.Minute}
// HTTP/2.0 200 OK
//fmt.Println(download(r)) // *mock.Retriever &{番茄炒蛋 1m0s}
fmt.Printf("%T %v\n", r, r)
rr := test.Retriever{"this is fake contents"}
// this is fake contents
//fmt.Println(download(rr)) // test.Retriever {this is fake contents}
fmt.Printf("%T %v\n", rr, rr)
}
获取接口的类型
case方式访问
package mainimport (
"fmt"
"learngo/retriever/mock"
"learngo/retriever/test"
"time"
)type Retriever interface {
Get(url string) string
}func download(r Retriever) string {
return r.Get("https://blog.csdn.net/qq_43135259/article/details/123298792?spm=1001.2014.3001.5502")
}func main() {
var r Retriever = &mock.Retriever{"番茄炒蛋", time.Minute}
// HTTP/2.0 200 OK
//fmt.Println(download(r)) // UserAgent: 番茄炒蛋
funcName(r) if mockRetriever, ok := r.(*mock.Retriever);
ok {
// TimeOut: 1m0s
fmt.Println("TimeOut:", mockRetriever.TimeOut)
}else {
fmt.Println("not a mock Retriever")
} r = test.Retriever{"this is fake contents"}
// this is fake contents
//fmt.Println(download(rr)) // ontents: this is fake contents
funcName(r)}func funcName(r Retriever) {
switch v := r.(type) {
case *mock.Retriever:
fmt.Println("UserAgent:", v.UserAgrent)
case test.Retriever:
fmt.Println("contents:", v.Contents)
}
}
文章图片
文章图片
- 接口变量自带指针
- 接口变量同样采用值传递,因为接口变量里面有一个指针,所以几乎不需要使用接口的指针
- 指针接收者实现只能以指针的方式来使用; 值接收者两者都可以
- Type Assertion
- Type Switch
- 表示任何类型:interface{} (go语言任何类型表示法)
推荐阅读
- 【第三十六期】B站一面
- 一些零碎代码|几十行代码,打造一款JavaScript便签系统
- 微服务学习|Nginx学习笔记
- Java基础的查漏补缺|Java基础查漏补缺之你可能不知道的final关键字的知识点
- #|《Effective Modern C++》学习笔记 - Item 29: 移动操作的“坑点”(它们可能不存在,开销不小或不会被调用)
- Java基础总结|Java期末复习速成(七)
- Java基础总结|Java期末复习速成(六)
- Java毕设项目|基于JavaScript+html5的家教小程序的设计与实现
- java|Java基础总结