在Go语言中,可以通过调用系统命令来查看所有进程。下面是一个示例代码:
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ps", "-ef") output, err := cmd.Output() if err != nil { fmt.Println("Failed to execute command:", err) return } fmt.Println(string(output)) }
上述代码使用了ps
命令来查看所有进程,并将输出打印出来。你可以根据需要对输出进行处理,比如解析进程ID等信息。请注意,在不同的操作系统上,ps
命令的参数可能会有所不同,你可能需要根据具体的操作系统进行调整。