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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教