- Printing version of the utility if started with special flag (like -V)
- Program needs complicated turn up through the command line arguments or configuration file, but normally is running on the background. It would be beneficially to have a choice either run it with console or run it quietly.
Nowadays I found myself programming more and more in Go. While initially Go compiler was intended for Linux only, that is not the case anymore. Windows now is the first class citizen in the Go word. Recently I needed to create application, which may open the console if that demanded by command line argument, but work invisibly otherwise . To my surprise there were not that many online information how to do that in Go. I did find one answered question in StackOverflow. Alas, proposed solution did not work for me out of the box. But using it as starting point and after some googling I have found workable solution. Here is sample of Go application, which will allocate console, print some prompt, wait for keyboard input and quit.
I would like to point some details about syscall.Syscall invocation for AllocConsole :
- This function has five arguments. It is different of what you will find in the online Go Doc . Looks like doc references Linux definition, which is not the same as Windows.
- Second parameter here is number of arguments in dll function. For our case it is 0.
- Function returns three variables, but only first is really meaningful. It is the result returned by AllocConsole function. Second variables always 0. Third variables is an error, but in cannot be analyzed in the regular Go way as far as it is never nil. Still it could be useful: in case of success its value is different from value on failure (for example if application was build with no windowsgui flag and already has the console ) .