Home > Backend Development > Golang > How Can I Execute Commands in Go Without Showing the Command Prompt Window?

How Can I Execute Commands in Go Without Showing the Command Prompt Window?

Susan Sarandon
Release: 2024-12-04 15:18:11
Original
307 people have browsed it

How Can I Execute Commands in Go Without Showing the Command Prompt Window?

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
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template