英文:
Runtime Error: Index out of range when attempting to os.StartProcess
问题
我似乎无法弄清楚为什么会出现这种情况:
我有一个设置如下的函数:
func (srv *Server) StartServer() {
// 确保路径正确的一些操作
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 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
编辑:链接到一个简化的 play.golang 示例,可以重现该问题。我正在运行 go 版本 1.4.2 win/amd64。
http://play.golang.org/p/S6kRLMyd2I
英文:
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
答案1
得分: 2
你之所以出现错误,是因为你没有为os.ProcAttr的Stderr和Stdout设置文件描述符。在Linux上,这些似乎会自动设置,但在Windows上,你需要手动设置它们。
以下是一个可行的示例:
func (srv *Server) StartServer() {
// 确保路径正确的一些操作
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)
}
}
希望对你有帮助!
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论