英文:
Understanding Interface
问题
type Info interface {
Noofchar() int
}
type Testinfo struct {
noofchar int
}
func (x Testinfo)Noofchar() int {
return x.noofchar
}
func main(){
var t Info
fmt.Println(x.Testinfo)
fmt.Println("No of char ",t.Noofchar())
x.noofchar++
fmt.Println("No of char ",t.Noofchar())
}
英文:
I am a newbie in Go lang , I was trying to understand Go Interface by writing a simple piece of code . I am getting an error , as I am not able to understand the correct way of referring to a Interface method kindly tell me where I am going wrong.
type Info interface {
Noofchar() int
}
type Testinfo struct {
noofchar int
}
func (x Testinfo)Noofchar() int {
return x.noofchar
}
func main(){
var t Info
fmt.Println(x.Testinfo)
fmt.Println("No of char ",t.Noofchar())
x.noofchar++
fmt.Println("No of char ",t.Noofchar())
}
Am I referring to the method correctly by t.Noofchar() ? or there is something else that I am missing
答案1
得分: 3
方法通常接收结构体的指针。
func (x Testinfo)Noofchar() int {
改为
func (x *Testinfo)Noofchar() int {
在开头删除了var x Info
,稍微重构了你的main()
函数,得到的代码如下:
package main
import "fmt"
type Info interface {
Noofchar() int
Increment()
}
type Testinfo struct {
noofchar int
}
func (x *Testinfo) Noofchar() int {
return x.noofchar
}
func (x *Testinfo) Increment() {
x.noofchar++
}
func main(){
var t Info = &Testinfo{noofchar:1}
fmt.Println("No of char ",t.Noofchar())
t.Increment()
fmt.Println("No of char ",t.Noofchar())
}
http://play.golang.org/p/6D-LzzYYMU
在你的示例中,你直接修改了x
。如果你在传递接口时,你无法访问底层的数据结构,只能访问方法。所以我将你的直接递增改为了Increment()
方法。
英文:
Methods typically receive pointers to a struct.
func (x Testinfo)Noofchar() int {
changed to
func (x *Testinfo)Noofchar() int {
Took out var x Info
in the beginning, refactored your main()
just a bit and the resulting code in play is:
package main
import "fmt"
type Info interface {
Noofchar() int
Increment()
}
type Testinfo struct {
noofchar int
}
func (x *Testinfo) Noofchar() int {
return x.noofchar
}
func (x *Testinfo) Increment() {
x.noofchar++
}
func main(){
var t Info = &Testinfo{noofchar:1}
fmt.Println("No of char ",t.Noofchar())
t.Increment()
fmt.Println("No of char ",t.Noofchar())
}
http://play.golang.org/p/6D-LzzYYMU
In your example you modify x
directly. If you're passing an interface around, you don't have access to the underlying data structures, only the methods. So I changed your direct increment to an Increment()
method.
答案2
得分: 1
x
是一个变量,你可以将任何实现Info
接口的东西赋值给它。你还没有给这个变量赋值。
一旦你赋值了某个东西,x.noofchar++
将不起作用,因为x
可以保存任何实现Info
接口的东西,这意味着你只能访问由该接口定义的方法。接口不允许直接访问字段。
Info
接口中唯一定义的方法是Noofchar() int
方法,所以这是与存储在x
中的值交互的唯一方式。
方法接收器(x Testinfo)
定义的x
与var x Info
变量没有任何关系。这个x
确实可以直接访问结构字段。
t.Noofchar()
的调用是正确的,将起作用。
英文:
x
is a variable to which you can assign anything that implements the Info
interface. You've assigned nothing to that variable.
Once you assign something, x.noofchar++
will not work because again x
can hold anything that implements the Info
interface, which means you can only access the methods defined by that interface. Interfaces do not allow direct access to fields.
The only method defined in the Info
interface is the Noofchar() int
method, so that is the only way to interact with the value stored in x
.
The x
defined by the method receiver (x Testinfo)
isn't at all related to the var x Info
variable. That x
does have direct access to the struct fields.
The t.Noofchar()
calls are correct and will work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论