Home Backend Development Golang How to hide the dos window when golang calls cmd command

How to hide the dos window when golang calls cmd command

Dec 24, 2019 pm 04:14 PM
cmd command golang transfer hide

How to hide the dos window when golang calls cmd command

When calling the cmd command through go's standard library exec, a black window will pop up. To solve this problem, you can use the win32 API WinExec under Windows.

This problem mainly occurs when a program with UI or no console calls cmd.

Add parameters when compiling go:

go build  -ldflags="-H windowsgui"
Copy after login
package main
import (
	"errors"
	"
)
import (	
    "github.com/CodyGuo/win"
)
var (
	winExecError = map[uint32]string{		
	0:  "The system is out of memory or resources.",		
	2:  "The .exe file is invalid.",		
	3:  "The specified file was not found.",		
	11: "The specified path was not found.",
	}
)
func main() {
	err := execRun("cmd /c start http://www.baidu.com")	
	if err != nil {
		log.Fatal(err)
	}
}
func execRun(cmd string) error {
	lpCmdLine := win.StringToBytePtr(cmd)	
//http://baike.baidu.com/link?url=51sQomXsIt6OlYEAV74YZ0JkHDd2GbmzXcKj_4H1R4ILXvQNf3MXIscKnAkSR93e7Fyns4iTmSatDycEb
HrXzq
	ret := win.WinExec(lpCmdLine, win.SW_HIDE)	
	if ret <= 31 {		
	    return errors.New(winExecError[ret])
	}	return nil
}
Copy after login

Recommended related articles and tutorials: golang tutorial

The above is the detailed content of How to hide the dos window when golang calls cmd command. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pool for Golang database connection?

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

How to safely read and write files using Golang?

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Similarities and Differences between Golang and C++

How steep is the learning curve of golang framework architecture? How steep is the learning curve of golang framework architecture? Jun 05, 2024 pm 06:59 PM

How steep is the learning curve of golang framework architecture?

How to generate random elements from list in Golang? How to generate random elements from list in Golang? Jun 05, 2024 pm 04:28 PM

How to generate random elements from list in Golang?

Comparison of advantages and disadvantages of golang framework Comparison of advantages and disadvantages of golang framework Jun 05, 2024 pm 09:32 PM

Comparison of advantages and disadvantages of golang framework

What are the best practices for error handling in Golang framework? What are the best practices for error handling in Golang framework? Jun 05, 2024 pm 10:39 PM

What are the best practices for error handling in Golang framework?

golang framework document usage instructions golang framework document usage instructions Jun 05, 2024 pm 06:04 PM

golang framework document usage instructions

See all articles