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 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误