亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運行時錯誤:嘗試 os.StartProcess 時索引超出范圍

運行時錯誤:嘗試 os.StartProcess 時索引超出范圍

Go
MMTTMM 2021-10-18 14:32:24
我似乎無法弄清楚為什么要這樣做:我有一個這樣的功能設置: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)  }}StartProcess 方法拋出一個超出范圍的索引。我可能只是錯過了一些東西,但我就是看不到它。要求的確切錯誤:panic: runtime error: index out of rangegoroutine 1 [running]:syscall.StartProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08201dd60, 0x0, 0x0, 0x0, 0x0)        c:/go/src/syscall/exec_windows.go:322 +0x94cos.startProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08200a730, 0x5217e0, 0x0, 0x0)        c:/go/src/os/exec_posix.go:45 +0x482os.StartProcess(0xc082052b70, 0x21, 0xc08200a6e0, 0x5, 0x5, 0xc08200a730, 0x0, 0x0, 0x0)        c:/go/src/os/doc.go:24 +0x79main.(*Server).StartServer(0x5efae0)        E:/build_test/SrvMgr.go:85 +0x4e6main.main()        E:/build_test/SrvMgr.go:54 +0x141goroutine 2 [runnable]:runtime.forcegchelper()        c:/go/src/runtime/proc.go:90runtime.goexit()        c:/go/src/runtime/asm_amd64.s:2232 +0x1goroutine 3 [runnable]:runtime.bgsweep()        c:/go/src/runtime/mgc0.go:82runtime.goexit()        c:/go/src/runtime/asm_amd64.s:2232 +0x1goroutine 4 [runnable]:runtime.runfinq()        c:/go/src/runtime/malloc.go:712runtime.goexit()        c:/go/src/runtime/asm_amd64.s:2232 +0x1exit status 2編輯:鏈接到重現它的簡化 play.golang 帖子。我正在運行 go version 1.4.2 win/amd64http://play.golang.org/p/S6kRLMyd2I
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

您收到錯誤是因為您沒有在 os.ProcAttr 上設置 Stderr 和 Stdout 的文件描述符。似乎這些會在 linux 上自動設置,但您需要在 Windows 上設置它們。


這是一個工作示例:


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)

  }

}


查看完整回答
反對 回復 2021-10-18
  • 1 回答
  • 0 關注
  • 425 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號