Hey guys,
Sorry if this is a dumb topic, but I'm not fluent in the cgo implementation yet. First of all, below is the code I'm trying to run:
package main
/*
typedef struct {
char *name;
} foobar_t;
foobar_t foobar_load(char *name) {
foobar_t loaded = {name};
return loaded;
};
*/
import "C"
import "fmt"
type Foobar C.struct_foobar_t
func setFoobar(name string) Foobar {
return C.foobar_load(C.CString(name))
}
func main() {
fmt.Println(C.GoString(setFoobar("fuuuu").name))
}
When I run this code using go run, I get the following error:
# command-line-arguments
./foobar.go:19: cannot use _Cfunc_foobar_load(_Cfunc_CString(name)) (type C.struct___0) as type Foobar in return argument
./foobar.go:23: setFoobar("fuuuu").name undefined (type Foobar has no field or method name)
I could be wrong, but what I think it's happening is that cgo interprets and informs the compiler that foobar_t is just a simple struct and should be read as C.struct___x by the compiler. This case just happens when I define the struct with typedef then set the function type as it.
If I define the return type of setFoobar() as C.struct___0, voilà:
But that's not what I need, as I'm creating a go implementation for a really complex and typeful C code.
Any ideas? I'd be very grateful if someone shows up with cool solutions, as I'm working on this for some time now. :-D
Thanks a lot,
Stephano