Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Pass a slice of custom type from Go to C#

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't know how C# invoke Go, let's assume we are going to invoke Go function from C code.

  1. You need define Res struct in C file. like cgo_interface.h

  2. 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
}
  1. Then use go build --buildmode=c-archive cinvokego.go

  2. Then you will get header file and archive file generated by go build.

  3. Then you use header and archive like normal C resource.

  4. Then use C# to invoke C resource.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!