douzhuang6321 2016-11-27 11:00
浏览 92
已采纳

为什么在仍然可以直接访问的情况下,接口不使用指针引用实现方法?

I do understand that interface don't implement method with pointer reference as per Go spec and FAQ, as T and *T have different method sets (https://golang.org/doc/faq#guarantee_satisfies_interface).

So, this doesn't work:

package main

import (
    "fmt"
)

type user struct {
    name string
}

type modifier interface {
    modify()
}

func (u *user) modify() {
    u.name = "My name"
}

func interfaceModify(m modifier) {
    m.modify()
}

func main() {
    u := user{"Default Name"}

    interfaceModify(u)
    fmt.Println(u.name)
}

and returns:

./main.go:26: cannot use u (type user) as type modifier in argument to interfaceModify: user does not implement modifier (modify method has pointer receiver)

This is explained as:

[…]there is no useful way for a method call to obtain a pointer.

Even in cases where the compiler could take the address of a value to pass to the method, if the method modifies the value the changes will be lost in the caller.

However, replacing interfaceModify(u) with a direct call like u.modify() does work: the compiler takes the address of u, and will modify its name property as Println() confirms.

So, we are able to do that operation in that precise case, but not in the interface one. My only explanation of that difference is that in interfaceModify(m modifier), we will have a direct copy of u to m, and no way for m to match corresponding address when calling modify(). And, so, declaring u := &user{"Default Name"} would thus copy the pointer (so address) u to m, and that's why m.modify() is possible. Am I correct?

  • 写回答

2条回答 默认 最新

  • dongyou2714 2016-11-27 14:09
    关注

    I think you've got it. u.modify() works because go sees it as shorthand for (&u).modify(). interfaceModify(&u) would also work. Here's a playground with some more examples of pass by reference vs. value.

    https://play.golang.org/p/HCMtcFAhLe

    package main
    
    import (
        "fmt"
    )
    
    type user struct {
        name string
    }
    
    type userPointer struct {
        user
    }
    
    func (up *userPointer) modify() {
        up.name = "My name"
    }
    
    type modifier interface {
        modify()
    }
    
    func (u user) modify() {
        u.name = "My name"
    }
    
    func interfaceModify(m modifier) {
        m.modify()
    
    }
    
    func main() {
        u := user{"Default Name"}
        u.modify()
        fmt.Println(u.name)
        interfaceModify(u)
        fmt.Println(u.name)
    
        up := userPointer{user{"Default Name"}}
        interfaceModify(&up)
        fmt.Println(up.name)
        // short hand version
        up.name = "Default Name"
        up.modify()
        fmt.Println(up.name)
        // long hand version https://golang.org/ref/spec#Calls
        up.name = "Default Name"
        (&up).modify()
        fmt.Println(up.name)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题