【发布时间】:2014-01-01 15:55:52
【问题描述】:
此代码无法编译 - type *IF does not have method MyMethod:
1 package main
2
3 type IF interface {
4 MyMethod(i int)
5 }
6
7 type AType struct {
8 I *IF
9 }
10
11 func (a *AType) aFunc() {
12 a.I.MyMethod(1)
13 }
但是,如果 I 本身被定义为 IF,则编译正常:
1 package main
2
3 type IF interface {
4 MyMethod(i int)
5 }
6
7 type AType struct {
8 I IF // not a pointer
9 }
10
11 func (a *AType) aFunc() {
12 a.I.MyMethod(1)
13 }
在我的例子中,I 应该是指向实现IF 的记录的指针,但为什么不允许呢?
【问题讨论】:
标签: go