Golang call cmd under program hide Black Window-Method 1

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The call to cmd command via the Go Standard library exec will flash the Black window, in order to resolve this issue under Windows can be used with the Win32 API WinExec.

This problem occurs primarily when a program that has a UI or no console calls CMD.

Add parameters when compiling go: Go build-ldflags= "-H Windowsgui"


Package Mainimport ("Errors" "Log") 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 is 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_ 4h1r4ilxvqnf3mxiscknaksr93e7fyns4itmsatdycebhrxzqret: = win. WinExec (lpCmdLine, win.sw_hide) if RET <= {return errors. New (Winexecerror[ret])}return Nil}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.