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 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入