如何获取第三方进程的参数

huangapple go评论66阅读模式
英文:

How to get the arguments of a third-party process

问题

有一个进程,例如 test.exe,它使用参数 --argument1 --argument2 启动,并且这些参数可以通过任务管理器看到。
问题:我如何通过 Golang 获取这些参数?

如何获取第三方进程的参数

英文:

There is a process, for example test.exe , it is launched with arguments --argument1 --argument2 and they are visible through the task manager.
Question: How do I get these arguments through golang?

如何获取第三方进程的参数

答案1

得分: 1

我找到了解决方案。

import (
    "github.com/shirou/gopsutil/v3/process"
)

func main() {
    processes, _ := process.Processes()
    for _, process := range processes {
        cmd, _ := process.Cmdline()
        fmt.Println(cmd)
    }
}

这是一个Go语言的代码片段,它使用了github.com/shirou/gopsutil/v3/process包来获取系统中所有进程的命令行信息,并将其打印出来。

英文:

I found solution.

import (
"github.com/shirou/gopsutil/v3/process"
)

func main(){
processes, _ := process.Processes()
for _, process := range processes {
	cmd, _ := process.Cmdline()
	fmt.Println(cmd)
    }
}

答案2

得分: 0

程序实际上是通过标志启动的:--arg1=value --arg2=anotherValue

我更倾向于使用Go的flag来采用惯用的方法。

最好避免通过添加外部包来增加程序的复杂性,特别是当Go已经有适合你需求的东西时。

这里有一个用法示例。

英文:

The program is actually launched with flags: --arg1=value --arg2=anotherValue

I'd rather suggest an idiomatic approach using Go's flag package.

It's always better to avoid increasing complexity to your programs by adding external packages, specially when Go has something for your need.

Here is a usage example.

huangapple
  • 本文由 发表于 2023年2月24日 07:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定