-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: please provide a way to set errno from a callback #23890
Comments
/cc @ianlancetaylor |
This is doable in principle--we could apply special handling to an //extern function that returns two values where the second one has type |
I think go also needs the ability to set errno when calling into certain functions like From the macOS man page for
It seems the only way to do this is to write a C function to set errno. |
As I said earlier, this case is so rarely used that I don't think it's worth the extra complexity in cgo. For people who need this I recommend writing, in the cgo comment. /*
#include <errno.h>
#include <sys/types.h>
#include <pwd.h>
static struct passwd *goGetpwent() {
errno = 0;
return getpwent();
}
*/
import "C" Then change your code to call |
I'm the original reporter, and with four years more experience, I fully agree with Ian. Sorry for the noise. |
I'm interfacing with a C library that expects callbacks to return -1 with
errno
set upon error. As far as I can tell, the only way to set errno in a go function is to call back into C:This is in contrast with retrieving
errno
in Go using multiple return values, which I find elegant and Go-like. Please provide a similarly tasteful way to return from Go to C while settingerrno
.The text was updated successfully, but these errors were encountered: