如何保留 Go 中的工厂函数?

发布于 2024-11-06 05:35:41 字数 898 浏览 3 评论 0原文

我试图保存一个函数,该函数将返回在结构中实现特定接口的类型的新实例。这是一个精简的示例:

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 conversion newAdder (type func() *number) to type NewAdder。那么实现我的目标的类似方法是什么?

这是我正在尝试做的一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-11-13 05:35:41
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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文