GithubHelp home page GithubHelp logo

matiasinsaurralde / go-dotnet Goto Github PK

View Code? Open in Web Editor NEW
469.0 30.0 42.0 1.93 MB

Go wrapper for the .NET Core Runtime.

License: MIT License

Go 44.25% C++ 50.75% C 3.61% C# 0.44% Shell 0.95%
dotnet wrapper golang dotnet-core

go-dotnet's Introduction

go-dotnet

GoDoc MIT License Build status

This is a PoC Go wrapper for the .NET Core Runtime, this project uses cgo 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 ExecuteManagedAssembly.

  • Load a .dll, setup delegates and call them from your Go functions.

I've tried calling both C# and VB.NET methods, of course you need to generate the assembly first, check below for more details!

Note: After some tweaks it seems to work fine under Linux! Remember to install the SDK first :)

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 TRUSTED_PLATFORM_ASSEMBLIES, it will use the default tpaList value.
		APP_PATHS & NATIVE_DLL_SEARCH_DIRECTORIES 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{
		// "TRUSTED_PLATFORM_ASSEMBLIES": "/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",
		// "NATIVE_DLL_SEARCH_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 dmcs (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 net/http samples, like this.
  • Provide useful callbacks.
  • Support blittable types.
  • CSharpScript support.
  • Code generation tool (with go generate), a few notes here.
  • Add tests.

I'm open to PRs, Go/.NET swag, suggestions, etc.

Additional resources

Build Status

Linux 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

Windows / Go 1.9 / .NET Core 2.x

Windows build status

License

MIT

go-dotnet's People

Contributors

bbigras avatar gracig avatar matiasinsaurralde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-dotnet's Issues

.NET Standard 2.0

Interesting work. I am also playing around with this idea.

.NET Standard 2.0 can out a few days ago and i jumped on it because it provides a base for using c# cross OS (Win, MAC; IOS , Android, Linux).

https://github.com/dotnet/standard/blob/master/docs/versions.md

Its Microsofts new way of getting c# working everywhere and so seems to me to be the best base moving forward on your project perhaps.
Let me know what you think ....

So in order to use this new stuff there are a few things that need to be changed in your code.


x-MacBook-Pro:gedw99 apple$ dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help            Display help.
  --version         Display version.

path-to-application:
  The path to an application .dll file to execute.
x-MacBook-Pro:gedw99 apple$ dmcs
Note: dmcs is deprecated, please use mcs instead!
error CS2008: No files to compile were specified
Compilation failed: 1 error(s), 0 warnings

How to setup delegate to return byte[] ?

Thank you very much for your excellent package.
But as I don't know C, I face the following problem:
There are typedefs in test.go for C# methods with signatures int A(int, int) and string S(). That's OK.
But I need to return array of bytes (byte[]) from my C# method and consume it from Go function.
What C# method signature and corresponding typedef to use ?
To return slice of bytes from Go function with the help of C.GoBytes I need unsafe.Pointer and C.int. How to obtain these ?
Could you provide an example ?

Callback support

Considering this project hasn't seen any activity in quite some time, Im not hopeful for an answer.
But I would like to know if there is any kind of support for making callbacks from the .NET code to the Go.

In the example in the readme file, there is a way to call .NET code, but no example how to make the .NET code call the Go code back.

Windows support

This is currently in progress here.
Main issue is that the UNIX hosting API isn't compatible with the Windows one, that's what I can conclude from my tests.

I can't open a dll written in c#

Hello great work!

I was trying to call a method written in c#, but when i run i get the next error.

# github.com/matiasinsaurralde/go-dotnet
could not determine kind of name for C.getSize
could not determine kind of name for C.initDelegateArguments
could not determine kind of name for C.pushDelegateArgument

The dll of c# is in the same folder. The implementation code is the next.

propeties := map[string] string{
		"TRUSTED_PLATFORM_ASSEMBLIES": "C:\\Program Files (x86)\\Reference
Assemblies\\Microsoft\\Framework\\.NETCore\\v4.5",
		"APP_PATHS": "C:\\Users\\Sergio\\Desktop\\TerminalGO",
		"NATIVE_DLL_SEARCH_DIRECTORIES": "C:\\Users\\Sergio\\Desktop\\TerminalGO",
	}
	runtime, errorso := dotnet.NewRuntime(dotnet.RuntimeParams{
		Properties: propeties,
		ManagedAssemblyAbsolutePath: "SumaString.dll"})

	fmt.Sprintln(runtime)
	defer runtime.Shutdown()

	if errorso != nil {
		panic(errorso)
	}

The c# code is the next

namespace SumaString
{
    public class SumaString
    {
        public string Sumar(string n1, string n2)
        {
            return (Convert.ToDouble(n1) + Convert.ToDouble(n2)).ToString();
        }
    }
}

Need i to indicate the .net libraries or can be another error?

TRUSTED_PLATFORM_ASSEMBLIES line maintainability questioned

https://github.com/matiasinsaurralde/go-dotnet/blob/master/examples/http.go#L22

I understand you're producing miracles here to make .NET interoperate with golang and that's well-appreciated.

The only beef I have is the maintainability of certain lines to make it happen.
TRUSTED_PLATFORM_ASSEMBLIES and what you assign to it makes it very difficult to isolate what changes over time on that one line. There are too many things happening on it.
I understand by doing so it will load up faster but I imagine later you're going to want to place more trusted platform assemblies and different versions of them might conflict with each other which is where I believe a great deal of effort will be spent examining differences if left in its current one-line FOR EVERYTHING state. It's the long-term maintainability of that one line I question.

undefined: dotnet method

Hi,
I'm build your example and I get this two errors
Do you know what I'm doing wrong?
Thank you very much

./go-dotnet.go:32:18: undefined: dotnet.NewRuntime
./go-dotnet.go:32:36: undefined: dotnet.RuntimeParams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.