Linux 系统调用是直接使用的,无需加载库,具体如何使用取决于您要执行哪个系统调用。
我将使用 Linux 系统调用 getpid() 作为示例,它返回调用进程(在本例中是我们的进程)的进程 ID。
package main
import (
"fmt"
"syscall"
)
func main() {
pid, _, _ := syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0)
fmt.Println("process id: ", pid)
}
我在 pid 中捕获系统调用的结果,这个特定的调用没有返回错误,所以我对其余的返回使用空白标识符。系统调用返回两个 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/ https://golang.org/pkg/syscall/