如何在Go中保留一个工厂函数?

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

How do I hold onto a factory function in Go?

问题

我正在尝试编写一个函数,该函数将在一个结构体中返回实现特定接口的类型的新实例。以下是一个简化的示例:

package main

type Adder interface {
    Add(Adder)
    Val() int
}

type NewAdder func() Adder

type number int

type NewHolder struct {
    newFunc NewAdder
}

func (me *number) Add(it Adder) {
    *me += number(it.Val())
}

func (me *number) Val() int {
    return int(*me)
}

func newAdder() *number {
    return (*number)(new(int))
}

func main() {
    var holder NewHolder
    holder.newFunc = NewAdder(newAdder)
}

当我尝试编译它时,它显示cannot convert newAdder (type func() *number) to type NewAdder。那么,有什么符合Go语言习惯的方法可以实现我的目标吗?

以下是一个Python示例,供参考:

class NewStuffMaker(object):
    def __init__(self, new):
        self.new = new

def make_new():
    return []
obj = NewStuffMaker(make_new)
new_lst = obj.new()

理想情况下,我希望能够在一个切片中保存这些函数(或包含函数和其他数据的结构体)。这样做是否合理?

英文:

I'm trying to hold a function that will return new instances of a type implementing a particular interface in a struct. Here's a stripped down example:

package main

type Adder interface {
	Add(Adder)
	Val() int
}

type NewAdder func() Adder

type number int

type NewHolder struct {
	newFunc NewAdder
}

func (me *number)Add(it Adder) {
	*me += number(it.Val())
}

func (me *number)Val() int {
	return int(*me)
}

func newAdder() *number {
	return (*number)(new(int))
}

func main() {
	var holder NewHolder
	holder.newFunc = NewAdder(newAdder)
}

When I try to compile it, it says cannot convert newAdder (type func() *number) to type NewAdder. So what's the go-like way to accomplish my goal?

Here's a Python example of what I'm trying to do, just for reference:

class NewStuffMaker(object):
    def __init__(self, new):
        self.new = new

def make_new():
    return []
obj = NewStuffMaker(make_new)
new_lst = obj.new()

Ideally, I'd like to be able to hold a bunch of these functions (or structs containing the functions and some other data) in a slice. Is that an unreasonable thing to do?

答案1

得分: 1

package main

type Adder interface {
Add(Adder)
Val() int
}

type FuncAdder func() Adder

type Holder struct {
FuncAdder
SomeOtherData string
}

type number int

func (n *number) Add(a Adder) {
*n += number(a.Val())
}

func (n *number) Val() int {
return int(*n)
}

func NumberAdder() Adder {
return Adder(new(number))
}

func main() {
var holders []Holder
var holder Holder
holder.FuncAdder = NumberAdder
holder.SomeOtherData = "SomeOtherData"
holders = append(holders, holder)
}

英文:
package main

type Adder interface {
	Add(Adder)
	Val() int
}

type FuncAdder func() Adder

type Holder struct {
	FuncAdder
	SomeOtherData string
}

type number int

func (n *number) Add(a Adder) {
	*n += number(a.Val())
}

func (n *number) Val() int {
	return int(*n)
}

func NumberAdder() Adder {
	return Adder(new(number))
}

func main() {
	var holders []Holder
	var holder Holder
	holder.FuncAdder = NumberAdder
	holder.SomeOtherData = "SomeOtherData"
	holders = append(holders, holder)
}

huangapple
  • 本文由 发表于 2011年5月8日 14:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/5926003.html
匿名

发表评论

匿名网友

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

确定