Go 入门指南(The way to Go)int) { 14. *l = append(*l, val) 15. } 16. 17. type Appender interface { 18. Append(int) 19. } 20. 21. func CountInto(a Appender, start, end int) { 22. for i := start; i <= end; i++ compiler error: 39. // cannot use lst (type List) as type Appender in argument to CountInto: 40. // List does not implement Appender (Append method has pointer receiver) 41. // CountInto(lst 时会导致一个编译器错误,因为 CountInto 需要一个 Appender ,而它的方法 Append 只定义在指针上。 在 lst 上调用 LongEnough 是可以的因为 ‘Len’ 定义在值上。 在 plst 上调用 CountInto 是可以的,因为 CountInto 需要一个 Appender ,并且它的方法 Append 定义在指针上。0 码力 | 466 页 | 4.44 MB | 1 年前3
The Way To Go - 2012len(l) } func (l *List) Append(val int) { *l = append(*l, val) } type Appender interface { Append(int) } func CountInto(a Appender, start, end int) { 276 Ivo Balbaert for i := start; i <= end; lst List // compiler error: // cannot use lst (type List) as type Appender in function argument: // List does not implement Appender (Append method requires pointer // receiver) // CountInto(lst Discussion: CountInto called with the value lst gives a compiler error because CountInto takes an Appender, and Append() is only defined for a pointer. LongEnough on value lst works because Len() is defined0 码力 | 629 页 | 4.85 MB | 1 年前3
Go 入门指南(The way to Go)func (l *List) Append(val int) { *l = append(*l, val) } type Appender interface { Append(int) } func CountInto(a Appender, start, end int) { for i := start; i <= end; i++ { a.Append(i) // compiler error: // cannot use lst (type List) as type Appender in argument to CountInto: // List does not implement Appender (Append method has pointer receiver) // CountInto(lst, 1 CountInto 时会导致一个编译器错误,因为 CountInto 需要一个 Appender ,而它的 方法 Append 只定义在指针上。 在 lst 上调用 LongEnough 是可以的因为 'Len' 定义在值上。 在 plst 上调用 CountInto 是可以的,因为 CountInto 需要一个 Appender ,并且它的方法 Append 定义在指针上。 在 plst0 码力 | 380 页 | 2.97 MB | 1 年前3
共 3 条
- 1













