Golang:如何在Linux上使用syscall.Syscall? [英] Golang: How to use syscall.Syscall on Linux?

查看:138
本文介绍了Golang:如何在Linux上使用syscall.Syscall?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在Windows上使用syscall包加载共享库和调用函数的描述非常不错(

There is a very nice description about loading a shared library and calling a function with the syscall package on Windows (https://github.com/golang/go/wiki/WindowsDLLs). However, the functions LoadLibrary and GetProcAddress that are used in this description are not available in the syscall package on Linux. I could not find documentation about how to do this on Linux (or Mac OS).

感谢帮助

推荐答案

直接使用Linux系统调用而无需加载库,确切地说,取决于您要执行哪个系统调用.

Linux syscalls are used directly without loading a library, exactly how depends on which system call you would like to perform.

我将以Linux syscall getpid()为例,它返回调用进程(在本例中为我们的进程)的进程ID.

I will use the Linux syscall getpid() as an example, which returns the process ID of the calling process (our process, in this case).

package main

import (
    "fmt"
    "syscall"
)

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

我在pid中捕获了syscall的结果,这个特定的调用没有返回错误,因此我在其余的返回中使用空白标识符. Syscall返回两个uintptr和1个错误.

I capture the result of the syscall in pid, this particular call returns no errors so I use blank identifiers for the rest of the returns. Syscall returns two uintptr and 1 error.

如您所见,我也可以为其余的函数参数传入0,因为我不需要将参数传递给此syscall.

As you can see I can also just pass in 0 for the rest of the function arguments, as I don't need to pass arguments to this syscall.

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

The function signature is: func Syscall(trap uintptr, nargs uintptr, a1 uintptr, a2 uintptr, a3 uintptr) (r1 uintptr, r2 uintptr, err Errno).

有关更多信息,请参见 https://golang.org/pkg/syscall/

For more information refer to https://golang.org/pkg/syscall/

这篇关于Golang:如何在Linux上使用syscall.Syscall?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆