0

I have a C function which will return an array of structure to go function. How can I receive the array of structure and interpret or cast to go structure?

Here is the code snippet

typedef struct student{  
    nameStruct name;  
    addressStruct address;  
} studentStruct;

typedef struct name{
    char firstName[20];
    char lastName[20];
} nameStruct;

typedef struct address{
    char location[40];
    int    pin;
}addressStruct;


student* getAllStudents(){
   //Allocate memory for N number of students
   student *pStudent= (struct student*)(N* sizeof(struct student));
   //populate the array
   return pStudent;
}

I need to get the pStudent array in my go code

package main

/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lkeyboard
#include "keyboard.h"
*/
import "C"
import (
    "fmt"
)

type student struct {
    name string
    ID int
}

func main() {
    s := student{} //Need to know how to decide the length of the struct array
    s = C.getAllStudents() //?????


}

Can some one help me with code snippet?

7
  • There's a lot of missing functionality here, covering a lot of basic areas. What part are you asking about? Commented Oct 12, 2016 at 20:14
  • what is the best way of casting C structure into Go structure? considering there can be different data types in the C structure. Commented Oct 13, 2016 at 6:19
  • If the data types are different, you need to copy/convert those to their respective fields. For example, Go strings are not the same as C strings, so you can't "cast" those, you need to copy the characters from the C char* to the Go string Commented Oct 13, 2016 at 12:52
  • I am aware of that, that's why I was looking for any other short cut which can directly cast or map C struct to Golang struct Commented Oct 14, 2016 at 5:27
  • I have added a complex C struct studentStruct in my original post. Can some one add a code snippet to interpret this C struct in Go code? Commented Oct 16, 2016 at 15:37

1 Answer 1

1

You could use the out paramters, and the way for C struct -> Go struct is thus:

package main

/*
#include <stdlib.h>

typedef struct Point{
    float x;
    float y;
}Point;

void GetPoint(void **ppPoint) {
   Point *pPoint= (Point *)malloc(sizeof(Point));
   pPoint->x=0.5f;
   pPoint->y=1.5f;
   *ppPoint = pPoint;
}
*/
import "C"

import "unsafe"

type Point struct {
    x float32
    y float32
}

func main() {
    var ppoint unsafe.Pointer
    C.GetPoint(&ppoint)
    point := *(*Point)(ppoint)
    println(point.x, point.y)
}
Sign up to request clarification or add additional context in comments.

2 Comments

In fact, I suggest use json string between C and Golang.
converting all different data types to json string before sending to Golang will take additional transformation logic what I was trying to avoid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.