英文:
How can I run a function in a structure that implements an interface?
问题
大家好,我来帮你翻译这段代码:
package main
import (
"fmt"
"math"
)
func main() {
var structObj *Struct
Obj1 := Obj1{}
Obj2 := Obj2{}
structObj = New(&Obj1)
fmt.Println(structObj.name)
structObj = New(&Obj2)
fmt.Println(structObj.runFunc)
}
type IStruct interface {
Id() string
Name() string
Run() string
}
type Obj2 struct {
}
func (self *Obj2) Id() string {
return "Obj2Id"
}
func (self *Obj2) Name() string {
return "Obj2"
}
func (self *Obj2) Run() string {
return "Obj2 run func"
}
type Obj1 struct {
}
func (self *Obj1) Id() string {
return "Obj1id"
}
func (self *Obj1) Name() string {
return "Obj1"
}
func (self *Obj1) Run() string {
return "Obj1 run func"
}
type Function func() string
type Struct struct {
name string
runFunc Function
isCompleted bool
}
func New(impl IStruct) *Struct {
return &Struct{
name: impl.Name(),
runFunc: impl.Run,
isCompleted: false,
}
}
输出结果
Obj1
0x6f6e00
当我调用实现了IStruct
接口的结构体的名称时,它可以正常工作,但是当我调用结构体的runFunc
时,它返回一个十六进制地址。我应该怎么做才能在调用runFunc
时执行它?
我在这里最想知道的最后一件事是,当我想将实现了特定接口的结构体作为接口使用时,为什么要给出结构体的地址?
英文:
Hi guys i have this code
package main
import (
"fmt"
"math"
)
func main(){
var structObj *Struct
Obj1 := Obj1{}
Obj2 := Obj2{}
structObj = New(&Obj1)
fmt.Println(structObj.name)
structObj = New(&Obj2)
fmt.Println(structObj.runFunc)
}
type IStruct interface {
Id() string
Name() string
Run() string
}
type Obj2 struct {
}
func (self *Obj2) Id() string {
return "Obj2Id"
}
func (self *Obj2) Name() string {
return "Obj2"
}
func (self *Obj2) Run() string {
return "Obj2 run func"
}
type Obj1 struct {
}
func (self *Obj1) Id() string {
return "Obj1id"
}
func (self *Obj1) Name() string {
return "Obj1"
}
func (self *Obj1) Run() string {
return "Obj1 run func"
}
type Function func() string
type Struct struct {
name string
runFunc Function
isCompleted bool
}
func New(impl IStruct) *Struct {
return &Struct{
name: impl.Name(),
runFunc: impl.Run,
isCompleted: false,
}
}
Output
Obj1
0x6f6e00
When i call the names of structs that implements IStruct it works but when i call runFunc from structs it returns hex adress what should i do to run when i call runFuncs.
The last thing I wonder here is when I want to use a structure that implements a certain interface as an interface, I give the address of the structure, why does this happen?
答案1
得分: 1
你的runFunc
是一个函数。打印一个函数只会打印它的地址。
如果你想打印它的输出,你需要实际执行它:
fmt.Println(structObj.runFunc())
另一方面,name
是一个字符串,所以打印它的工作方式与预期完全相同。
至于你的第二个问题,你需要调用New(&Obj1)
,因为实现接口的所有方法都使用指针接收器:(self *Obj1)
。
这意味着*Obj1
实现了接口,但Obj1
没有。如果你希望它实现接口,你可以将所有的接收器改为值接收器:(self Obj1)
,然后你就可以调用New(Obj1)
。
英文:
Your runFunc
is a function. Printing a function is just printing its address.
If you want to print its output, you need to actually execute it:
fmt.Println(structObj.runFunc())
On the other hand, name
is a string, so printing it works exactly as expected.
As for your second question, you need to call New(&Obj1)
because all the methods implementing the interface are using pointer receivers: (self *Obj1)
.
This means that *Obj1
implements the interface, but Obj1
does not. If you want it to, you can change all your receivers to value receivers: (self Obj1)
and you will be able to call New(Obj1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论