Home > Backend Development > Golang > golang calls exe method

golang calls exe method

PHPz
Release: 2023-05-10 12:06:07
Original
2510 people have browsed it

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.

  1. Execute command

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

This command will output "hello world" on the terminal.

  1. Execution program

You can also execute other executable files using the Command function. For example:

cmd := exec.Command("calc")
Copy after login

This command will start the calculator program on Windows systems.

  1. Pass parameters

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

This command will ping the google server on the terminal and output 4 results.

  1. Execute the command and obtain the result

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

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.

  1. Windows API

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

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!".

  1. Linux system call

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

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template