golang其interface类型, 实现了动态绑定. 

 1 import "fmt"
 2 
 3 type ITest interface {
 4     Test()
 5 }
 6 
 7 type A int
 8 
 9 func (a *A) Test() {
10     fmt.Println(a)
11 }
12 
13 func print(t ITest) {
14     fmt.Printf("%T", t)
15 }
16 
17 func main() {
18     a := A(1)
19     print(&a)
20 }

输出结果是: 

*main.A

尤其注意, main函数中, print(&A), 如果传入A, 返回错误: "method has pointer receiver"

这里面的差别, 可以参考 http://blog.csdn.net/timemachine119/article/details/54927121

 

相关文章:

  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-12-28
猜你喜欢
  • 2022-01-14
  • 2022-12-23
  • 2021-09-15
  • 2021-11-15
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
相关资源
相似解决方案