“方法需要指针接收器”在 Go 编程语言中

发布于 2024-12-04 17:32:08 字数 623 浏览 0 评论 0原文

我刚刚看到了 Go 编程语言的演示,并想尝试写几行。一切工作正常,直到我尝试在这种情况下使用界面。我该如何解决这个问题?

package main

import "fmt"

type entity float32

func (e *entity) inc() {
    *e++
}

type incer interface {
    inc()
}

func doSomething(i incer) {
    i.inc()
}

func main() {
    fmt.Println("Hello, 世界")

    var e entity = 3
    e.inc()
    doSomething(e)
    fmt.Println(e)
}

我收到编译器错误:

prog.go:24: cannot use e (type entity) as type incer in function argument:
entity does not implement incer (inc method requires pointer receiver)

我想使用指针,以便 inc() 影响函数外部的实体。我应该使用什么语法?

/瑞奇

I just saw a presentation of the Go programming language and thought I'd try to write a few lines. Everything worked fine until I tried to use an interface in this situation. How do I solve this?

package main

import "fmt"

type entity float32

func (e *entity) inc() {
    *e++
}

type incer interface {
    inc()
}

func doSomething(i incer) {
    i.inc()
}

func main() {
    fmt.Println("Hello, 世界")

    var e entity = 3
    e.inc()
    doSomething(e)
    fmt.Println(e)
}

I get the compiler error:

prog.go:24: cannot use e (type entity) as type incer in function argument:
entity does not implement incer (inc method requires pointer receiver)

I want to use a pointer so that the inc() will affect the enity outside the function. What is the syntax I should use?

/Ricky

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

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

发布评论

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

评论(2

将军与妓 2024-12-11 17:32:08

我认为这里有些混乱。 inc*entity 类型的方法,而不是 entity 类型的方法(虽然您可以直接在指针上调用值的方法;您通常不能直接在值上调用指针上的方法)。您可能会感到困惑的是为什么您可以调用 e.inc(),而不是必须执行 (&e).inc()。这是一个鲜为人知的特殊情况,记录在该语言的 Calls 部分的底部规范,表示如果 x 是可寻址的,并且 &x 的方法集包含 m,则 xm() 是简写(&x).m()。这适用于这种情况,因为 e 是一个变量,因此它是可寻址的;但其他表达式可能无法寻址。但是,我建议您不要使用此快捷方式,因为它会导致混乱;它让你认为e符合接口inter,但事实并非如此。

I think there is some confusion here. inc is a method of the type *entity, and not of the type entity (while you can call methods on values directly on pointers; you cannot generally call methods on pointers directly on values). What you may be confused about is why you could call e.inc(), instead of having to do (&e).inc(). This is a little-known special case documented at the bottom of the Calls section in the language specification, that says if x is addressable, and &x's method set contains m, then x.m() is shorthand for (&x).m(). This applies to this case because e is a variable, so it is addressable; but other expressions may not be addressable. I would recommend that you not use this shortcut, however, as it causes confusion; it makes you think that e conforms to the interface inter, while it does not.

仙气飘飘 2024-12-11 17:32:08

将其更改为:doSomething(&e)。 func (e *entity) inc() 仅满足 *entity 类型的 incer 接口。没有仅用于实体类型的 inc() ,这就是您传递给 doSomething() 的内容。

Change it to: doSomething(&e). func (e *entity) inc() satisfies incer interface only for *entity type. There is no inc() for just entity type and that's what's you're passing to doSomething().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文