In Go, when employing syscall to obscure command line windows using exec.Command(name, args...), certain users encounter a lingering problem: the window reappears after compilation in Windows environments.
This question seeks to uncover a solution to prevent the command line window from materializing. While the technique of compiling Go source into Windows GUI executables using go build -ldflags -H=windowsgui effectively suppresses the launch window for the program itself, Exec continues to produce visible windows.
Solution:
A superior solution exists that empowers exec.Command() to execute without displaying a visible window.
import syscall cmd_path = "C:\Windows\system32\cmd.exe" cmd_instance = exec.Command(cmd_path, "/c", "notepad") cmd_instance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} cmd_output, err := cmd_instance.Output()
This code imports syscall and assigns the command path, arguments, and HideWindow attribute to the cmd_instance object. Subsequently, it executes the command and stores the output in cmd_output.
This approach successfully conceals command line windows while utilizing exec.Command() in Go, addressing the initial query effectively.
The above is the detailed content of How to Hide Command Prompt Windows When Using `exec.Command` in Go?. For more information, please follow other related articles on the PHP Chinese website!