duan2477 2017-05-10 04:19
浏览 43
已采纳

类型断言后如何使用指针接收器调用方法?

I am learning interface, type conversions and methods with pointer receivers. The rules and terminology behind pointer receiver methods are confusing to me. Let me demonstrate my confusion with one program.

This is my Go program.

package main

import "fmt"

type Employee struct {
    Name string
}

func (e Employee) Hi() {
    fmt.Printf("Hi! I am %s.
", e.Name)
}

func (e *Employee) Hello() {
    fmt.Printf("Hello! I am %s.
", e.Name)
}

func main() {
    var a Employee = Employee{"Alice"}
    a.Hi()
    a.Hello()

    var b interface{} = Employee{"Bob"}
    b.(Employee).Hi()
    // b.(Employee).Hello()
}

This is the output.

Hi! I am Alice.
Hello! I am Alice.
Hi! I am Bob.

If I remove the last commented out line, I get this error.

# command-line-arguments
./foo.go:24: cannot call pointer method on b.(Employee)
./foo.go:24: cannot take the address of b.(Employee)

How can I fix that line of code so that I am able to invoke the method with pointer receiver? Please explain a solution with some clarification on why this does not work by laying down the concepts of methods with pointer receiver.

  • 写回答

2条回答 默认 最新

  • dragon7088 2017-05-10 04:33
    关注

    You can't (in this case implicitly for a pointer receiver) take the address of the result of an expression (b.(Employee)). You can take the address of a variable. For example,

    package main
    
    import "fmt"
    
    type Employee struct {
        Name string
    }
    
    func (e Employee) Hi() {
        fmt.Printf("Hi! I am %s.
    ", e.Name)
    }
    
    func (e *Employee) Hello() {
        fmt.Printf("Hello! I am %s.
    ", e.Name)
    }
    
    func main() {
        var a Employee = Employee{"Alice"}
        a.Hi()
        a.Hello()
    
        var b interface{} = Employee{"Bob"}
        b.(Employee).Hi()
        // b.(Employee).Hello()
        // main.go:24: cannot call pointer method on b.(Employee)
        // main.go:24: cannot take the address of b.(Employee)
        e := b.(Employee)  // e, a variable, is addressable
        e.Hello()
    
        var c interface{} = &Employee{"Chris"}
        c.(*Employee).Hi()
        c.(*Employee).Hello()
    }
    

    Output:

    Hi! I am Alice.
    Hello! I am Alice.
    Hi! I am Bob.
    Hello! I am Bob.
    Hi! I am Chris.
    Hello! I am Chris.
    

    The Go Programming Language Specification

    Type assertions

    For an expression x of interface type and a type T, the primary expression

    x.(T)
    

    asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

    If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs.

    Calls

    A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()

    Address operators

    For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.

    The value of the type assertion b.(Employee) is of type Employee. The method call b.(Employee).Hello() is shorthand for (&b.(Employee)).Hello() since func (e *Employee) Hello() has a pointer receiver. But, b.(Employee), an expression, is not addressable. Therefore,

    error: cannot call pointer method on b.(Employee)
    error: cannot take the address of b.(Employee)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重