How to invoke a method with pointer receiver after type assertion?
go, interface, pointers, type-conversion
Solution
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.\n", e.Name)
}
func (e *Employee) Hello() {
fmt.Printf("Hello! I am %s.\n", 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)
Problem
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.\n", e.Name) } func (e *Employee) Hello() { fmt.Printf("Hello! I am %s.\n", 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.