How to detect whether my Go process is running in a headless environment?

I have a Go program which in case it can create a GUI it should, and if it can’t, it should just skip that part. Therefore, I want to know if the Go program is running in a headless environment (a GUI cannot be created). The GUI library that I use doesn’t throw an error but terminates the process (which is a different issue that I’ve already raised with the library).

So, what is the best way to detect whether my Go process is running in a headless environment?

(Because I already asked this question on a different platform, and got no response there, I have meanwhile created my own library for this at but maybe somebody has already implemented something like this, and in a better way.)

Well, you would need to know what operating system you are on first I think.

Couldn’t you perhaps just try to create a window? Presumably whatever GUI package you are using will give you an error if it can’t do it.

Yes the implementation of this is certainly OS specific. But knowing the operating system is exactly what I want to avoid in my own code and delegate to a library.

Just trying to create a window and catch the error is something that doesn’t work with the UI package that made me look into this in the first place. The UI package that I use ( will terminate the process if its UI operation fails. That’s a separate issue, and I filed a bug for that UI package.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.