Hiding Command Prompt Window with Exec in Go
When executing commands within Go using the exec.Command function, it's desirable to avoid displaying the command prompt window. While setting SysProcAttr.HideWindow to true typically hides it, you might encounter issues on Windows.
Solution:
To effectively prevent the command prompt window from appearing, consider the following solution:
import ( "os/exec" "syscall" ) // HideWindow invokes a command with hidden command prompt. func HideWindow() error { cmd_path := "C:\Windows\system32\cmd.exe" cmd_instance := exec.Command(cmd_path, "/c", "notepad") cmd_instance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} _, err := cmd_instance.Output() return err }
Note: This solution originates from [Reddit](https://www.reddit.com/r/golang/comments/2c1g3x/build_golang_app_reverse_shell_to_run_in_windows/).
The above is the detailed content of How Can I Execute Commands in Go Without Showing the Command Prompt Window?. For more information, please follow other related articles on the PHP Chinese website!