languages | products | page_type | name | urlFragment | description | |||
---|---|---|---|---|---|---|---|---|
|
|
sample |
Calling Go functions from C# code |
golang-shared-c-dotnet-cs |
A simple example of using golang's cgo library and .NET's P/Invoke to call Go functions from C# code like a C-style API. |
brew install FiloSottile/musl-cross/musl-cross brew install mingw-w64
Go has a library called cgo that enables the creation of Go packages that call C code. One of cgo's coolest features is that it enables Go packages to be compiled into C shared libraries, exposing Go functions as a C-style APIs. This sample shows how we can compile a Go project into a Shared C .dll and call Go functions from within a .NET Core project using .NET's P/Invoke.
This project consists of two primary components:
- The .NET Core app that imports and calls functions from the Shared C libraries. This code is found in
SharedC/Program.cs
. - The Go packages that define the imported functions and are compiled into the Shared C libraries. This code is located in
Go/src/shared-c/cmd/*
. There are 2 Go projects, helloworld and math. Each deal with different use cases and exhibits how to write C-style APIs in Go that handle a variety of data types and situations.
Another part of the repo is the GoBuilder project. This lives in Go/
and is responsible for automating the building of the Go libraries. On build, GoBuilder executes a build-all powershell script that builds all of the Go packages in the repo. If errors occur when building the Go libraries the Go build logs can be found in Go/GoBuilder/bin/<BuildConfig>/netstandard2.0
.
-
.NET Core 3.1 - download
-
Golang v1.12+ - You must have Golang installed on your machine to build the Golang executables. If you do not have Golang installed, check out install Golang and for Windows machines, download and install the Microsoft Windows .msi. Follow the installation instructions on the download page.
-
GCC Compiler - In Windows we need a runtime environment for GCC to support binaries native to Windows 64/32-bit operating systems. The recommended tool for this is MinGW. MinGW (Minimalist GNU for Windows) is a minimalist development environment for native Microsoft Windows applications. Install MinGW here, and follow these installation instructions. You only need to follow instruction through the "Prerequisites" section. Make sure that when you install, you install for your specific architecture (i.e. x86_64 (AMD64)). You will need to find and add gcc.exe to your PATH.
Before running the SharedC project you must build the solution first. This will automatically build the 2 Go packages used in this sample (helloworld and math) and add them to SharedC/bin/<BuildConfig>/netcoreapp3.1
. By default, the project is configured to build only the Windows .dll's. If you want to build the Go libraries on a Linux machine you will need to change the target operating system in the GoBuilder powershell scripts. One of the pitfalls of cgo is cross platform compilation, it is not super simple. There are ways to make cross compilation work with cgo and if you are interested in cgo cross platfrom compilation this article will help with the proper environment configurations.
Once the solution has been successfully built, all you need to do is run the SharedC project in VisualStudio. A console will pop up, execute the imported Go functions, and output the results. You can debug the Program.cs
as well if you want to step through and get a better idea of what is going on.