I am trying to understand cgo. I can call C functions from go and the other way also. I tried doing something different and it didn't work.
go code:
// main.go
package main
/*
extern void myfunction(GoSlice s);
*/
import "C"
func main() {
var s [5]byte
C.myfunction(s[:])
}
C code:
// main.c
#include "_cgo_export.h"
#include <stdint.h>
void myfunction(GoSlice s)
{
for (int i = 0; i < s.len; i++)
printf("%hhu ", ((uint8_t *)s.data)[i]);
}
The compilation fails because GoSlice is not defined at the time of the C function declaration in main.go. Also, I can't include _cgo_export.h in main.go to get the definition. Is there any way I can get this working ? Note that I can get the functionality working using C compatible data types. But I want to directly use go data type here.