Go学习|Go学习:接口的值类型

接口变量 接口的定义和实现
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) } }

Go学习|Go学习:接口的值类型
文章图片

Go学习|Go学习:接口的值类型
文章图片

  • 接口变量自带指针
  • 接口变量同样采用值传递,因为接口变量里面有一个指针,所以几乎不需要使用接口的指针
  • 指针接收者实现只能以指针的方式来使用; 值接收者两者都可以
【Go学习|Go学习:接口的值类型】查看接口变量
  • Type Assertion
  • Type Switch
  • 表示任何类型:interface{} (go语言任何类型表示法)

    推荐阅读