Closed tejas-kale closed 3 years ago
This is an intentional extension: since gophernotes
is interactive, the compile and runtime phases are interleaved
and allowing statements and expressions at top-level is both useful and necessary:
it's useful because gophernotes
can also be used as a calculator - for example you can execute 7 + 12
or "foo" + "bar"
it's also necessary because as minimum you need a way to execute the functions you created - there is no separate runtime that will execute your main()
[UPDATE] about "how" it works under the hood: for every declaration, statement or expression the steps are
string
to a sequence of go/token.Token
using a slightly modified go/scanner.Scanner
that also accepts top-level statements and expressionsgo/ast.Node
using a slightly modified go/parser
that also accepts top-level statements and expressionsgithub.com/cosmos72/gomacro/fast/Expr
@cosmos72 Thanks for the explanation. It helps me understand the difference between gophernotes
and the regular compile and run execution of Go. Closing the issue.
Hi,
I have just started learning Go and the following code (saved as
cards.go
), when executed using an IDE fails with the error./cards.go:5:1: syntax error: non-declaration statement outside function body
.But the same piece of code when run in a Jupyter cell works fine.
So, I am curious to know what happens under the hood in
gophernotes
which allows the Jupyter cell shown above to run fine. Thanks.