Passing structures from C to Go

Hi,

I am trying to pass a structure from C to Go and Access the structure elements at Go
here is a sample program, Can someone help me here to get the proper syntax and reference links.

.c code
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include “_cgo_export.h”

struct Sample {
int value;
char name[25];
};

void sayhi() {
printf(“In function sayhi\n”);
struct Sample s1;
strcpy(s1.name, “goodwill”);
s1.value = 12;
Gohi(val, s1);
}


.go code

package main

/*
#include<stdint.h>
void sayhi();
*/
import “C”

import “fmt”

//export Gohi
func Gohi(str *C.char, data1 C.struct_Sample) {
gostr := C.GoString(str)
value1 := C.GoString(data1.value)
fmt.Println("value from structure is: ", value1)
fmt.Println("string is: ", gostr)
fmt.Println("len of gostring: ", len(gostr))
}

func main() {
fmt.Println(“Hello from GO”)
C.sayhi()
fmt.Println(“END”)
}

getting the below error:

In file included from $WORK/_/tmp/roopa/_obj/cgo_export.c:2:0:
./gofunc.go:54:35: warning: ‘struct Sample’ declared inside parameter list [enabled by default]
./gofunc.go:54:35: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
/tmp/go-build086420181/
/tmp/roopa/_obj/cgo_export.c:9:28: warning: ‘struct Sample’ declared inside parameter list [enabled by default]
void Gohi(char* p0, struct Sample p1)
^
/tmp/go-build086420181/
/tmp/roopa/_obj/cgo_export.c:9:35: error: parameter 2 (‘p1’) has incomplete type
void Gohi(char* p0, struct Sample p1)
^
/tmp/go-build086420181/
/tmp/roopa/_obj/cgo_export.c: In function ‘Gohi’:
/tmp/go-build086420181/
/tmp/roopa/_obj/_cgo_export.c:14:17: error: field ‘p1’ has incomplete type
struct Sample p1;

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.