During the development process using golang, sometimes it is necessary to call external exe programs to complete some tasks. Here are some common golang methods for calling exe.
1. os/exec package
In the golang standard library, you can use the os/exec package to call external commands and programs. It provides a set of simple functions and structures to facilitate us to execute external commands.
You can use the Command function to create a Cmd structure and specify the command to be executed. For example:
cmd := exec.Command("echo", "hello world")
This command will output "hello world" on the terminal.
You can also execute other executable files using the Command function. For example:
cmd := exec.Command("calc")
This command will start the calculator program on Windows systems.
If you need to pass parameters to an external program, you can use the Args attribute to add parameters to the Cmd structure. For example:
cmd := exec.Command("ping", "-c", "4", "www.google.com")
This command will ping the google server on the terminal and output 4 results.
After using the Command function to execute the command, you can use the Output method to obtain the execution result. For example:
cmd := exec.Command("ls", "-l") output, err := cmd.Output() if err != nil { fmt.Println(err) } fmt.Println(string(output))
This command will list all files in the current directory on the terminal and output their detailed information.
2. syscall package
The os/exec package only supports calling command line programs. If you need to call advanced interfaces such as Windows API or Linux system calls, you can use the syscall package in the golang standard library.
Taking Windows as an example, if you need to call the Windows API, you can use the syscall.LoadLibrary and syscall.GetProcAddress functions to load the dynamic link library and obtain the API function . For example:
dll, err := syscall.LoadLibrary("user32.dll") if err != nil { fmt.Println(err) return } defer syscall.FreeLibrary(dll) proc, err := syscall.GetProcAddress(dll, "MessageBoxW") if err != nil { fmt.Println(err) return } lpText, _ := syscall.UTF16PtrFromString("Hello, World!") lpCaption, _ := syscall.UTF16PtrFromString("Message") syscall.Syscall( uintptr(proc), uintptr(0), uintptr(0), uintptr(unsafe.Pointer(lpText)), uintptr(unsafe.Pointer(lpCaption)), )
This code will call the MessageBoxW function in the Windows API in the program to pop up a pop-up window with the message "Hello, World!".
For systems such as Linux, you can also use the syscall package to call system commands or system functions. For example:
cmd := "/bin/ls" args := []string{"-l", "/"} env := os.Environ() err := syscall.Exec(cmd, args, env) if err != nil { log.Fatal(err) }
This code will call the /bin/ls command of the Linux system in the program and output all files in the root directory and their detailed information.
Summary:
The above are common ways for golang to call exe methods. The os/exec package can be used to easily and conveniently call command line programs, while the syscall package can more flexibly call high-level interfaces such as system commands, APIs, and functions. In actual development, different methods can be selected to complete the task according to the specific situation.
The above is the detailed content of golang calls exe method. For more information, please follow other related articles on the PHP Chinese website!