Obtaining the actual errno code when interfacing with C code via cgo

1,390 views
Skip to first unread message

akira82

unread,
Mar 13, 2013, 10:04:24 PM3/13/13
to golang-nuts
I'm experimenting with using cgo at the moment and am having trouble
getting the actual errno code. I know it's possible to get the errno
wrapped up nicely as a Go error like this:

ret, err := C.SomeFunc()

This doesn't provide me with the actual errno code though, only the
string representation. How do I get the actual code?

I found this thread
http://groups.google.com/group/golang-nuts/browse_thread/thread/85e98c1f6fe279b2/a8e754cf06e7ad1d?lnk=gst&q=cgo+errno#a8e754cf06e7ad1d

The solution no longer seems to work (at least for me).

main.go:17[/tmp/go-build631361673/command-line-arguments/_obj/
main.cgo1.go:19]: undefined: syscall.Error

I've also seen some mention of an os.Errno variable in Go, but also
have the same problem with that:

main.go:17[/tmp/go-build176917443/command-line-arguments/_obj/
main.cgo1.go:19]: undefined: os.Errno

I'm using Go installed direct from the repositories and building using
the 'go build' tool. The 'go version' command doesn't seem to provide
much in the way of detailed version information ('go version go1') for
me to provide, what information should I provide in the future when
asking for support?

Troy

Ian Lance Taylor

unread,
Mar 14, 2013, 12:33:21 AM3/14/13
to akira82, golang-nuts
On 3/13/13, akira82 <troy....@hixxy.org> wrote:
> I'm experimenting with using cgo at the moment and am having trouble
> getting the actual errno code. I know it's possible to get the errno
> wrapped up nicely as a Go error like this:
>
> ret, err := C.SomeFunc()
>
> This doesn't provide me with the actual errno code though, only the
> string representation. How do I get the actual code?

Convert to syscall.Errno.

Here is an example.

package main

/*
#include <errno.h>
int fn() { errno = EINVAL; return 0; }
*/
import "C"

import (
"fmt"
"syscall"
)

func main() {
i, err := C.fn()
var errno syscall.Errno
if err != nil {
errno = err.(syscall.Errno)
}
fmt.Println(i, err, int(errno))
}

On my system this prints

0 invalid argument 22

Ian
Reply all
Reply to author
Forward
0 new messages