因此,要终止 Ubuntu 机器上的进程,我可以向我的进程发送 syscall.SIGTERM
信号,但这在 Windows 上不起作用。
为了修复它,我已经切换到使用 syscall.TerminateProcess
但当然这在 Ubuntu 上不受支持。
有没有一种方法可以终止可在两个平台上运行的进程,或者我是否需要为 unix 和 windows 分别编写代码?
最佳答案
您可以使用 runtime.GOOS 在运行时确定您的操作系统.
由于您必须在某个时候为各自的平台编译代码,因此您可以非常轻松地编写特定于操作系统的代码。
所有相关文档都可以在 Go build 中找到.
If a file's name, after stripping the extension and a possible _test suffix, matches *_GOOS, *_GOARCH, or *_GOOS_GOARCH for any known operating system and architecture values, then the file is considered to have an implicit build constraint requiring those terms.
这基本上意味着例如名为 mypackage_windows.go
的文件只会在为 Windows 编译时包含。
关于windows - Go中终止进程的跨平台方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49527215/