duangaixing1509
2015-06-26 12:10
浏览 61
已采纳

运行时错误:尝试os.StartProcess时索引超出范围

I can't seem to figure out why it's doing this:

I have a function setup like this:

func (srv *Server) StartServer() {
  // Some stuff to make sure paths are correct

  path := srv.Path + "server.exe"
  var args = []string{
    "ip=" + srv.IP,
    "un=" + srv.Username,
    "pw=" + srv.Password
  }
  proc, err := os.StartProcess(path, args, new(os.ProcAttr))
  if err != nil {
    panic(err)
  }
}

The StartProcess method throws an index out of range.

I'm probably just missing something, but I just can't see it.

Exact error as requested:

panic: runtime error: index out of range

goroutine 1 [running]:
syscall.StartProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08201dd60, 0x0, 0x0, 0x0, 0x0)
        c:/go/src/syscall/exec_windows.go:322 +0x94c
os.startProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08200a730, 0x5217e0, 0x0, 0x0)
        c:/go/src/os/exec_posix.go:45 +0x482
os.StartProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08200a730, 0x0, 0x0, 0x0)
        c:/go/src/os/doc.go:24 +0x79
main.(*Server).StartServer(0x5efae0)
        E:/build_test/SrvMgr.go:85 +0x4e6
main.main()
        E:/build_test/SrvMgr.go:54 +0x141

goroutine 2 [runnable]:
runtime.forcegchelper()
        c:/go/src/runtime/proc.go:90
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
        c:/go/src/runtime/mgc0.go:82
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
        c:/go/src/runtime/malloc.go:712
runtime.goexit()
        c:/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

Edit: Link to a simplified play.golang post reproducing it. I'm running go version 1.4.2 win/amd64

http://play.golang.org/p/S6kRLMyd2I

图片转代码服务由CSDN问答提供 功能建议

我似乎无法弄清楚为什么这样做:

我有这样的功能设置:

  func(srv * Server)StartServer(){
 //一些确保路径正确的东西
 
 path:  = srv.Path +“ server.exe” 
 var args = [] string {
“ ip =” + srv.IP,
“ un =” + srv.Username,
“ pw =” + srv。 密码
} 
 proc,错误:= os.StartProcess(path,args,new(os.ProcAttr))
 if err!= nil {
 panic(err)
} 
} 
   
 
 

StartProcess方法将索引超出范围。

我可能只是丢失了一些东西,但我看不到它 。

所请求的确切错误:

 恐慌:运行时错误:索引超出范围
 
goroutine 1 [正在运行]:\  nsyscall.StartProcess(0xc082052b70、0x21、0xc08200a6e0、0x5、0x5、0xc08201dd60、0x0、0x0、0x0、0x0)
c:/go/src/syscall/execcall/exec_windows.go:322 + 0x94c 
os.startProcess(0xc082b  0xc08200a6e0、0x5、0x5、0xc08200a730、0x5217e0、0x0,  0x0)
c:/go/src/os/exec_posix.go:45 + 0x482 
os.StartProcess(0xc082052b70、0x21、0xc08200a6e0、0x5、0x5、0xc08200a730、0x0、0x0、0x0)
c:/ go / src /  os / doc.go:24 + 0x79 
main。(* Server).StartServer(0x5efae0)
 E:/build_test/SrvMgr.go:85 + 0x4e6 
main.main()
 E:/ build_test / SrvMgr。  go:54 + 0x141 
 
goroutine 2 [可运行]:
runtime.forcegchelper()
c:/go/src/runtime/proc.go:90 
runtime.goexit()
c:/ go / src / runtime  /asm_amd64.s:2232 + 0x1 
 
goroutine 3 [可运行]:
runtime.bgsweep()
c:/go/src/runtime/mgc0.go:82 
runtime.goexit()
c:/ go /  src / runtime / asm_amd64.s:2232 + 0x1 
 
goroutine 4 [可运行]:
runtime.runfinq()
c:/go/src/runtime/malloc.go:712 
runtime.goexit()
c:  /go/src/runtime/asm_amd64.s:2232 + 0x1 
exit状态2 
   
 
 

编辑:链接到简化的play.golang帖子,以重现它。 我正在运行1.4.2版win / amd64

http: //play.golang.org/p/S6kRLMyd2I

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtan1845 2015-06-26 13:21
    已采纳

    You are getting the error because you do not set the file descriptors for Stderr and Stdout on your os.ProcAttr. It seems those get set automatically on linux, but you need to set them on windows.

    This is a working example:

    func (srv *Server) StartServer() {
      // Some stuff to make sure paths are correct
    
      path := srv.Path + "server.exe"
      var args = []string{
        "ip=" + srv.IP,
        "un=" + srv.Username,
        "pw=" + srv.Password
      }
      var attr os.ProcAttr
      attr.Files = []*os.File{nil, os.Stdout, os.Stderr}
      proc, err := os.StartProcess(path, args, &attr)
      if err != nil {
        panic(err)
      }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题