如何在实现接口的结构体中运行一个函数?

huangapple go评论76阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2021年8月29日 20:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68973029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定