Go wrapper for the .NET Core Runtime.
go-dotnet
[![MIT License][license-image]][license-url] [![Build status][travis-build-image]][travis-build-status]
This is a PoC Go wrapper for the .NET Core Runtime, this project uses
and has been tested under OSX. It covers two basic use cases provided by the CLR Hosting API:
- Load and run an .exe, using its default entrypoint, just like corerun and coreconsole do, check
.
- Load a .dll, setup delegates and call them from your Go functions.
Note: After some tweaks it seems to work fine under Linux! Remember to install the SDK first :)
![Capture][capture]
An example
package main
import ( "github.com/matiasinsaurralde/go-dotnet"
"fmt" "os" )
func main() { fmt.Println("Hi, I'll initialize the .NET runtime.")
/* If you don't set the TRUSTEDPLATFORMASSEMBLIES, it will use the default tpaList value. APPPATHS & NATIVEDLLSEARCHDIRECTORIES use the path of the current program, this makes it easier to load an assembly, just put the DLL in the same folder as your Go binary! You're free to override them to fit your needs. */
properties := map[string]string{ // "TRUSTEDPLATFORMASSEMBLIES": "/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/mscorlib.ni.dll:/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Private.CoreLib.ni.dll", // "APP_PATHS": "/Users/matias/dev/dotnet/lib/HelloWorld", // "NATIVEDLLSEARCH_DIRECTORIES": "/Users/matias/dev/dotnet/lib/HelloWorld", }
/* CLRFilesAbsolutePath sets the SDK path. In case you don't set this parameter, this package will try to find the SDK using a list of common paths. It seems to find the right paths under Linux & OSX, feel free to override this setting (like the commented line). */
runtime, err := dotnet.NewRuntime(dotnet.RuntimeParams{ Properties: properties, // CLRFilesAbsolutePath: "/usr/share/dotnet/shared/Microsoft.NETCore.App/1.0.0" }) defer runtime.Shutdown()
if err != nil { fmt.Println("Something bad happened! :(") os.Exit(1) }
fmt.Println("Runtime loaded.")
SayHello := runtime.CreateDelegate("HelloWorld", "HelloWorld.HelloWorld", "Hello")
// this will call HelloWorld.HelloWorld.Hello :) SayHello() }
Preparing your code (C#)
I've used
(from Mono) to generate an assembly file, the original code was something like:
#
using System;
namespace HelloWorld {
public class HelloWorld { public static void Hello() { Console.WriteLine("Hello from .NET"); } }
}
And the command:
dmcs -t:library HelloWorld.cs
Preparing your code (VisualBasic)
I did a quick test with this program, using the VB.NET compiler from Mono:
vbnc -t:library HelloWorld.vb
I'm not sure about the status of Roslyn but it could be interesting to try it.
Setup
Coming soon!
Ideas
- Run some benchmarks.
- Add/enhance
samples, like this./http - Provide useful callbacks.
- Support blittable types.
- CSharpScript support.
- Code generation tool (with
go generate), a few notes here. - Add tests.
Additional resources
Build StatusLinux x64 / Go 1.9/1.10 / .NET Core 1.0/2.0 - OS X / Go 1.9/1.10 - .NET Core 1.0/2.0
[![Linux and OS X build status][travis-build-image]][travis-build-status]
Windows / Go 1.9 / .NET Core 2.x
[![Windows build status][windows-build-image]][windows-build-status]
License
[license-url]: LICENSE
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
[capture]: capture.png
[travis-build-image]: https://travis-ci.org/matiasinsaurralde/go-dotnet.svg?branch=master [travis-build-status]: https://travis-ci.org/matiasinsaurralde/go-dotnet
[windows-build-image]: https://ci.appveyor.com/api/projects/status/wc6hi7h89c6f83it/branch/windows-support?svg=true [windows-build-status]: https://ci.appveyor.com/project/matiasinsaurralde/go-dotnet/branch/windows-support