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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀