我是围棋的初学者。而我刚刚安装了gophernotes,打算主要使用Jupyter Notebook进行编程。
此程序在 Jupyter 中运行时出现以下错误:
Cell1: package main
Out1: runtime error: index out of range
Cell2: import "fmt"
func main() {
fmt.Println("hello world")
}
main()
Out2: hello world
当我在 test.go
中编写相同的内容并从 bash 执行时:go run test.go
,我得到以下信息:
Deepaks-MacBook-Air:JUPYTER deepak$ go run test.go
go: disabling cache (/Users/deepak/Library/Caches/go-build) due to initialization failure: open /Users/deepak/Library/Caches/go-build/log.txt: permission denied
# command-line-arguments
./test.go:6:1: syntax error: non-declaration statement outside function body
最佳答案
我认为拥有“package main”是个问题。 Go 与 Jupyter 一起工作的方式显然不同于 Go 单独工作的方式。您不需要 Jupyter 的包声明。
此外,您永远不应该调用 main()
。当您使用 go run
或 go build
运行程序时,这是自动完成的。
我不熟悉 Jupyter Notebook 以及它如何使用 Go。也许您确实需要调用该函数。如果是这种情况,请不要将您的函数命名为 main
,因为那样只会造成混淆。
根据我所见的 Jupyter/Go 示例,您不需要 Go 函数,只需列出代码即可。
关于go - 开始时遇到困难。 `package main` 引发运行时错误 - 索引超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53028812/