Golang:如何在 Linux 上使用 syscall.Syscall?

Golang: How to use syscall.Syscall on Linux?

Windows (https://github.com/golang/go/wiki/WindowsDLLs) 上关于加载共享库和使用系统调用包调用函数的描述非常好。但是,此描述中使用的函数 LoadLibraryGetProcAddress 在 Linux 上的系统调用包中不可用。我在 Linux(或 Mac OS)上找不到有关如何执行此操作的文档。

感谢帮助

Linux 直接使用系统调用而不加载库,具体如何取决于您要执行哪个系统调用。

我将使用 Linux 系统调用 getpid() 作为示例,其中 returns 调用进程(在本例中为我们的进程)的进程 ID。

package main

import (
    "fmt"
    "syscall"
)

func main() {
    pid, _, _ := syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0)
    fmt.Println("process id: ", pid)
}

我在 pid 中捕获系统调用的结果,这个特定的调用 returns 没有错误,所以我对 returns 的其余部分使用空白标识符。系统调用 returns 两个 uintptr 和 1 个错误。

如您所见,我也可以只为其余的函数参数传入 0,因为我不需要将参数传递给此系统调用。

函数签名为:func Syscall(trap uintptr, nargs uintptr, a1 uintptr, a2 uintptr, a3 uintptr) (r1 uintptr, r2 uintptr, err Errno)。

有关详细信息,请参阅 https://golang.org/pkg/syscall/