gpt4 book ai didi

go - 我如何从 Go 设置 errno

转载 作者:IT王子 更新时间:2023-10-29 01:26:49 24 4
gpt4 key购买 nike

我有一个 C 函数通过 cgo 调用 Go 例程。我需要 Go 例程来正确设置 errno,以便 C 线程可以检查它的 errno 并采取相应的行动。无法通过谷歌搜索如何通过 Go 设置 errno

最佳答案

澄清一下,您仍然可以通过通过 cgo 调用的 C 函数来设置它。

package main

// #include <errno.h>
// #include <stdio.h>
//
// void setErrno(int err) {
// errno = err;
// }
//
import "C"

func main() {
C.setErrno(C.EACCES)
C.perror(C.CString("error detected"))
C.setErrno(C.EDOM)
C.perror(C.CString("error detected"))
C.setErrno(C.ERANGE)
C.perror(C.CString("error detected"))
}

在我的系统上它输出

error detected: Permission denied
error detected: Numerical argument out of domain
error detected: Numerical result out of range

关于go - 我如何从 Go 设置 errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com