可以使用 os 包中的 Execuable 函数获取当前进程的可执行文件路径,然后使用 filepath 包中的 Base 函数获取文件名即为进程名称。
示例代码:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
executable, err := os.Executable()
if err != nil {
panic(err)
}
processName := filepath.Base(executable)
fmt.Println(processName)
}
输出:
myprocess




