Skip to content

[Question] - Pass struct/slice of structs from C# <---> Go #28

@aleqsss

Description

@aleqsss

Hello once again @Digital-512

As mentioned in #27, I'm now seeking assistance for the task of passing a struct and also a slice of structs, from C# to a CGO exported function, and returning a struct and also a slice of structs, from a CGO exported function to the calling C# code.

Consider the following example struct in Go:

type Person struct {
	Name             string
	Hobbies          []string
	FavouriteNumbers []int
	Age              int
	Single           bool
}

Which is then called as a single struct or a slice of structs:

person := Person{
	Name:             "John",
	Hobbies:          []string{"Golf", "Hiking", "Outdoors "},
	FavouriteNumbers: []int{1, 2, 3},
	Age:              21,
	Single:           false,
}

people := []Person{
	{"John", []string{"Golf", "Hiking", "Outdoors "}, []int{1, 2, 3}, 21, false},
	{"Doe", []string{"Cycling"}, []int{4}, 32, false},
	// etc...
}

Building on top of your other provided snippets, what would be a suitable way of doing this?
How would I go on about passing both a single struct but also a slice of structs from C# to a CGO exported Go function, and also return them? Such as these examples:

type Person struct {
	Name             string
	Hobbies          []string
	FavouriteNumbers []int
	Age              int
	Single           bool
}

//export Struct
func Struct(person StructFromCSharpToGo) structFromGoToCSharp {

	// Use struct
	// return structToCSharp
}

//export StructSlice
func StructSlice(people StructSliceFromCSharpToGo) structSliceFromGoToCSharp  {

	// Use struct slice
	// return struct slice
}

func main() {}

Just get back if you need me to clearify anything, as I do see that I can get quite confusing in my examples and attempts to explain, because of my lack of knowledge in the topics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions