Home > Backend Development > Golang > How to Hide Command Prompt Windows When Using `exec.Command` in Go?

How to Hide Command Prompt Windows When Using `exec.Command` in Go?

Mary-Kate Olsen
Release: 2024-11-30 01:25:12
Original
853 people have browsed it

How to Hide Command Prompt Windows When Using `exec.Command` in Go?

How to Conceal Command Prompt Windows with Exec

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()
Copy after login

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!

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