gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.8k stars 264 forks source link

Can not call struct method #174

Closed xdays closed 5 years ago

xdays commented 5 years ago

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 5 years ago

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

cosmos72 commented 5 years ago

fixed in commit 1c4b514e4b9e3d0f3cc9b9c01a0dfa440c1dd564

mrg0lden commented 3 years ago

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

cosmos72 commented 3 years ago

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