再看go的interface代码示例
2018-11-09 22:06
267 查看
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/83506568
代码:
[code]package main import "fmt" type Base interface { Input() int } type Dog struct { } func (p Dog) Input() int { fmt.Println("call input dog") return 100 } func main() { handler := func() Base { return Dog{} } fmt.Printf("type: %T, %v\n", handler(), handler().Input()) }
结果:
call input dog
type: main.Dog, 100
代码:
[code]package main import "fmt" type Base interface { Input() int } type Dog struct { } func (p Dog) Input() int { fmt.Println("call input dog") return 100 } func main() { handler := func() Base { return &Dog{} } fmt.Printf("type: %T, %v\n", handler(), handler().Input()) }
结果:
call input dog
type: *main.Dog, 100
代码:
[code]package main import "fmt" type Base interface { Input() int } type Dog struct { } func (p *Dog) Input() int { fmt.Println("call input dog") return 100 } func main() { handler := func() Base { return &Dog{} } fmt.Printf("type: %T, %v\n", handler(), handler().Input()) }
结果:
call input dog
type: *main.Dog, 100
代码:
[code]package main import "fmt" type Base interface { Input() int } type Dog struct { } func (p *Dog) Input() int { fmt.Println("call input dog") return 100 } func main() { handler := func() Base { return Dog{} } fmt.Printf("type: %T, %v\n", handler(), handler().Input()) }
结果:
# command-line-arguments
./a.go:17:40: cannot use Dog literal (type Dog) as type Base in return argument:
Dog does not implement Base (Input method has pointer receiver)
好好理解下。
阅读更多
相关文章推荐
- PHP interface(接口)的示例代码
- 1行Go代码实现反向代理的示例
- go语言实现聊天服务器的示例代码
- go调用c代码示例
- go mysql事务代码示例
- Go语言展现快速排序算法全过程的思路及代码示例
- go 简单代码示例执行
- Go channel 使用示例代码
- 年月日三级联动代码示例
- VBA中操作Excel的部分方法代码示例
- 导航跟随滚动条置顶移动示例代码
- php calender(日历)二个版本代码示例(解决2038问题)
- 快递100API接口调用代码示例
- 新版Microsoft All-In-One Code Framework发布下载 - 绑定第二版示例代码浏览器
- three.js实现3D模型展示的示例代码
- js 判断浏览器使用的语言示例代码
- Spring Boot如何动态创建Bean示例代码
- java逻辑操作符代码示例
- iOS开发数据库篇—SQL代码应用示例