如何将错误消息从 C 传递给 Go?

How do I pass an error message from C to Go?

我想将错误消息从 C 代码传递到 Go 堆栈。我如何使用 out 参数完成此操作?

如果有人能帮助我,我将不胜感激。

Any C function (even void functions) may be called in a multiple assignment context to retrieve both the return value (if any) and the C errno variable as an error (use _ to skip the result value if the function returns void).

然后在您的 C 代码中,您所要做的就是设置“全局”errno 变量。

package main


// #include <errno.h>
// int fortytwo()
// {
//      errno = ENOENT;
//      return 42;
// }
import "C"
import "fmt"

func main() {

    n, err = C.fortytwo()
}