Can not call struct method
xdays opened this issue · 4 comments
xdays commented
I can't run the following code
import "fmt"
type person struct{
first string
last string
}
func (s person) speak() {
fmt.Println("I am", s.first, s.last)
}
p1 := person{
first: "xdays",
last: "chen",
}
fmt.Println(p1)
p1.speak()
and the error messge is:
repl.go:60:1: not a package: "p1" in p1.speak <*ast.SelectorExpr>
here's result from play.golang.org https://play.golang.org/p/3MUTTha849C
cosmos72 commented
Confirmed, this happens if you put all the code in the same cell. I will work on it.
As a temporary workaround, you can split the code in multiple cells - if you put
func (s person) speak() {
fmt.Println("I am", s.first, s.last)
}
into its own cell, then in later cells p1.speak()
will work