I want to copy contents of GOLang structure to C Structure. Here i want the populated GO Struct (type test struct) to be copied to C structure test_c.
Have put the following logic. I have accessed C structure test_c in go file, as C.test_c, and tried to copy the content of test struct to C.test_c (p_c) by using C.GoString, but when ever i try to do this i get this error
Can anyone let me know what is the mistake that am doing, is there a better method to achive the same ?
./go_structure.go:30: cannot use p_go.a (type string) as type *C.char in argument to _Cfunc_GoString
./go_structure.go:30: cannot use _Cfunc_GoString(p_go.a) (type string) as type [10]C.uchar in assignment
./go_structure.go:31: cannot use p_go.b (type string) as type *C.char in argument to _Cfunc_GoString
./go_structure.go:31: cannot use _Cfunc_GoString(p_go.b) (type string) as type [10]C.uchar in assignment
Below is the code,
go_stucture.go
package main
import (
/*
#include "cmain.h"
*/
"C"
"fmt"
"unsafe"
)
type test struct {
a string
b string
}
func main() {
var p_go test
var p_c C.test_c
p_go.a = "ABCDEFGHIJ"
p_go.b = "QRSTUVXWYZ"
//fmt.Println(unsafe.Sizeof(p_c.a))
fmt.Println("In GO code\n")
fmt.Println("GO code structure Member:a=%s", p_go.a)
fmt.Println("GO code structure Member:b=%s", p_go.b)
p_c.a = C.GoString(p_go.a)
p_c.b = C.GoString(p_go.b)
fmt.Println("Call C function by passing GO structure\n")
C.cmain((*C.test_c)(unsafe.Pointer(&p_c)))
}
cmain.c
#include <stdio.h>
#include "cmain.h"
#include "_cgo_export.h"
void cmain(test_c *value) {
printf("Inside C code\n");
printf("C code structure Member:a=%s\n", value->a);
printf("C code structure Member:b=%s\n", value->b);
}
cmain.h
typedef struct {
unsigned char a[10];
unsigned char b[10];
}test_c;
void cmain(test_c* value);