接口变量
接口的定义和实现
package main
import (
'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))
}
package mock
import (
'net/http'
'net/http/httputil'
'time'
)
type Retriever struct {
UserAgrent string
TimeOut time.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)
}
package test
type Retriever struct {
Contents string
}
func (r Retriever) Get(url string) string {
return r.Contents
}
接口变量内容
package main
import (
'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
', 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
', rr, rr)
}
r和rr里面有一个类型,和自己真实的value
通过指针访问
package mock
import (
'net/http'
'net/http/httputil'
'time'
)
type Retriever struct {
UserAgrent string
TimeOut time.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)
}
package main
import (
'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
', 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
', rr, rr)
}
获取接口的类型
case方式访问
package main
import (
'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)
}
}
接口变量自带指针接口变量同样采用值传递,因为接口变量里面有一个指针,所以几乎不需要使用接口的指针指针接收者实现只能以指针的方式来使用;值接收者两者都可以
查看接口变量
TypeAssertionTypeSwitch表示任何类型:interface{}(go语言任何类型表示法)
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点