user2969402
user2969402

Reputation: 1251

Cross platform way to terminate process in Go

So to terminate a process on an Ubuntu machine, I can send a syscall.SIGTERM signal to my process, but that won't work on Windows. To fix it, I've switched to using syscall.TerminateProcess but of course that is not supported on Ubuntu. Is there a way to terminate a process that would work on both platform or will I need to have separate code for unix and for windows?

Upvotes: 0

Views: 522

Answers (1)

matthias krull
matthias krull

Reputation: 4429

You can determine your operating system at runtime using runtime.GOOS.

As you have to compile the code for the respective platform at some point, you could write OS specific code pretty easily.

All the relevant documentation is available in 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.

That basically means e.g. a file named mypackage_windows.go will only be included when compiling for windows.

Upvotes: 3

Related Questions