gpt4 book ai didi

go - 查询进程状态

转载 作者:IT王子 更新时间:2023-10-29 01:56:57 27 4
gpt4 key购买 nike

我有一个简单的程序并运行一个 exe 并随后检查 Exited 值,但它给我一个“ panic :运行时错误:无效内存地址或 nil 指针取消引用”错误,知道为什么吗?

package main

import (
"fmt"
"os/exec"
"time"
)

func main() {
prog:= exec.Command("path\to\exe")
prog.Dir = "path\to"
go prog.Run()
fmt.Println(prog.ProcessState.Exited())
time.Sleep(500 * time.Second)
}

最佳答案

documentation says :

ProcessState contains information about an exited process available after a call to Wait or Run.

主 goroutine 在 ProcessState 字段通过调用 goroutine 中的 Run() 设置为非 nil 值之前访问该字段。结果调用 Exited() 出现 panic 。

一个简单的解决方法是从主 goroutine 调用 Run()。

关于go - 查询进程状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com