douzai6337 2018-10-28 06:03
浏览 563
已采纳

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

I'm a complete beginner in go. And I just installed gophernotes and intend to use Jupyter Notebook for main programming.

This program gives the following error when run in Jupyter:

Cell1: package main
Out1: runtime error: index out of range
Cell2: import "fmt"
      func main() {
          fmt.Println("hello world")
      }
      main()
Out2: hello world

When I write the same in a test.go and execute from bash: go run test.go, I get the following:

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
  • 写回答

1条回答 默认 最新

  • doumei9832 2018-10-28 06:22
    关注

    I think that having "package main" is a problem. The way Go works with Jupyter is apparently different than how Go works on its own. You don't need a package statement with Jupyter.

    Also you should never call main(). That is done automatically when you run the program with go run or go build.

    I am not familiar with Jupyter Notebook and how it uses Go. Maybe you do need to call the function. If that is the case do not name your function main because that is simply confusing.

    From what I have seen of Jupyter / Go examples you don't need a Go function you can just list out the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog