gopherdata/gophernotes

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

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

fixed in commit 1c4b514

I don't know if this is related, but I get the same error when I try to call a method, colab

Hello @mrg0lden,
please open a new issue with the minimal code that triggers the error,
and also include the output of go env