I am trying to integrate a Go application inside a large C# project. By using CGO it is easy to export functions from Go to C#, especially when the Go function return a generic type.
However, I have the need to call a Go function which return a slice of custom type from C#, possibly casting the slice to a C# list or array.
The Go function look like this
func Myfunc(n []int, m float64) []Res{...}
where type Res is
type Res struct {
w IW
p []Prs
ti int64
ms int64
}
type Prs struct {
mmz float64
cge int
itntn int64
}
type IW struct {
ledg float64
redg float64
}
I could not find any documentation or example on the web, can I do this somehow?
I don't know how C# invoke Go, let's assume we are going to invoke Go function from C code.
You need define Res struct in C file. like cgo_interface.h
Then you write a cgo file like this:
package cinvokego
/*
#cgo CFLAGS: -I ${include}
#cgo LDFLAGS: -L ${libs}
#include "cgo_interface.h"
*/
import "C"
func GoFuncs(retArr *unsafe.Pointer, otherargs int) {
// allocate memory and fill the retArr in here
}
Then use go build --buildmode=c-archive cinvokego.go
Then you will get header file and archive file generated by go build.
Then you use header and archive like normal C resource.
Then use C# to invoke C resource.