开始时遇到困难。 `package main` 抛出 运行 时间错误 - 索引超出范围?

Having trouble getting started on go. `package main` throws run time error- index out of range?

我完全是围棋初学者。而我刚刚安装了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 rungo build 的程序时,这是自动完成的。

我不熟悉 Jupyter Notebook 以及它如何使用 Go。也许您确实需要调用该函数。如果是这种情况,请不要将您的函数命名为 main,因为那样只会让人感到困惑。

根据我所见的 Jupyter/Go 示例,您不需要 Go 函数,只需列出代码即可。