When writing golang applications, we often need to interact with the terminal. However, sometimes we may need to hide the terminal window, such as when running a job. In this post, we will discuss how to hide terminal in golang.
Why hide the terminal?
In many scenarios, hiding the terminal may be necessary:
How to hide the terminal?
In golang, we can use the syscall package to interact with the system to achieve hidden terminals.
The steps are as follows:
Import the syscall package
import "syscall"
Define the structure
var ( kernel32DLL = syscall.NewLazyDLL("kernel32.dll") procShowWindow = kernel32DLL.NewProc("ShowWindow") ) const ( SW_HIDE = 0 )
Calling system function
procShowWindow.Call(uintptr(hwnd), uintptr(SW_HIDE))
Among them, hwnd is the window identifier and SW_HIDE is the window hiding command.
Sample code
The following is a sample code for hiding a window using golang.
package main import ( "syscall" ) func main() { var hwnd syscall.Handle syscall.GetInputState() syscall.GetConsoleWindow() procShowWindow.Call(uintptr(hwnd), uintptr(SW_HIDE)) }
Summary
In golang, the hidden terminal needs to interact with the system and is implemented using the syscall package. Through the introduction of this article, you should learn how to hide the terminal in golang.
The above is the detailed content of Discuss how to hide terminal in golang. For more information, please follow other related articles on the PHP Chinese website!