【问题标题】:Is there any way to pass multiple arguments in interface [Golang]? [closed]有没有办法在接口 [Golang] 中传递多个参数? [关闭]
【发布时间】:2021-11-06 04:39:17
【问题描述】:

有没有办法传递多个参数? 在这里,我分享我正在尝试调试的代码;

package main

type I interface {
    M()
}
type F int

func (f, j F) M() {
}

func main() {
    var i I
    i = F(42, 67)
    describe(i)
    i.M()
}

错误: ./prog.go:10:15: 方法有多个接收器 ./prog.go:15:7: 转换为 F 的参数太多:F(42, 67)

【问题讨论】:

  • 你所描述的没有意义。一个方法只能有一个接收者。你想完成什么?
  • 如果你想将多个整数包装到F,让它成为一个结构体。
  • The Tour of Go (tour.golang.org) 描述了基本语法,你应该学习它。您的代码看起来像是通过对词法分析器/解析器进行模糊测试而生成的测试用例。
  • @Adrian Multiple dispatch 存在并且有时是一件明智的事情,只是 Go 不支持它。但无论如何,我不确定提问者是否想要它。
  • @Thomas 他们所描述的甚至看起来不像是多次调度,但这个问题还不清楚它到底想做什么。

标签: go interface parameter-passing


【解决方案1】:

目前尚不清楚您要完成什么,但这是我的最佳猜测:

package main

type I interface {
    M()
}
type F struct {
    f int
    j int
}

func (f F) M() {
}

func main() {
    var i I
    i = F{42, 67}
    describe(i)
    i.M()
}

【讨论】:

  • 看起来不错;谢谢你;我们可以不使用结构吗?
  • 您为什么要这样做?这就是结构的用途。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2013-06-19
  • 2019-03-21
  • 2019-08-15
  • 1970-01-01
  • 2022-06-10
相关资源
最近更新 更多