Skip to content

kevinmiles/go2cs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go2cs

Golang to C# Converter

Converts source code developed using the Go programming language (see Go Language Specification) to the C# programming language (see C# Language Specification).

Goals

  • Convert Go code into C# so that Go code can be directly used within .NET ecosystem.
    • This is the primary goal of go2cs.
  • Convert Go code into visually similar C# code -- see conversion strategies.
    • Go is a minimalist language, as such it provides high-level functionality provided by the compiler. Converted C# code will have more visible code than Go used to provide equivalent functionality, however most of this code will be behind the scenes in separate files using partial class functionality.
  • Convert Go units test to C# and verify results are the same (TBD).
    • For most unit tests defined in Go, it should be possible to create an equivalent converted C# unit test. In many cases it may also be possible to successfully compare "outputs" of both unit tests as an additional validation test.
  • Convert Go code into managed C# code.
    • Conversion always tries to target managed code, this way code is more portable. If there is no possible way for managed code to accomplish a specific task, an option always exists to create a native interop library that works on multiple platforms, i.e., importing code from a .dll/.so/.dylib. Even so, the philosophy is to always attempt to use managed code, i.e., not to lean towards native code libraries, regardless of possible performance implications. Simple first.

Project Status

Picking up this project again now that .NET 5.0 is forthcoming and some of the new changes are conducive to this project's original goals, such as publishing as a self-contained executable.

Converted code now targets .NET Core only, specifically version 3.1 and C# 8.0 with goal to support .NET 5.0 and C# 9.0 when it comes out. Many basic conversion now work. Currently, work to improve code conversions is progressing by walking through each of the behavioral testing projects.

As a new conversion strategy to cover various code use cases, sets of common Go sample code have been manually converted to C# using the current C# Go Library. As an example, all relevant code samples from the "Tour of Go" have been converted to C#, see converted code. Ultimately would like to see this in head-to-head mode using Try .NET, for example: go2cs Currently converted code will not execute with latest release of Try .NET (see posted issue). Will be watching for an update.

As releases are made for updated go2cs executables, this will also include updates to pre-converted Go Standard Library libraries for reference from NuGet.

Testing

Before attempting conversions it is important that Go coding environment is already properly setup, especially GOPATH environmental variable. See Getting Started with Go.

Current Go to C# code conversions reference compiled assemblies of the go2cs core library code from the configured GOPATH, specifically %GOPATH%\src\go2cs\. Run the deploy-gocore.bat script located in the go2cs\src folder to handle copying source to target path and then building needed debug and release assemblies.

Once a compiled version of the current go2cs core library has been deployed, you can test conversions. For example:

go2cs -o -i C:\Projects\go2cs\src\Tests\Behavioral\ArrayPassByValue

This will convert Go code to C#. You can then build and run both the Go and C# versions and compare results.

Debugging with Visual Studio: After running the deploy-gocore.bat script you can run conversion code from within Visual Studio by right-clicking on the go2cs project, selecting "Properties" then clicking on the "Debug" tab. In the "Application arguments:" text box you can enter the command line test parameters, e.g., -o -i -h C:\Projects\go2cs\src\Tests\Behavioral\ArrayPassByValue. When the active solution configuration targets "Debug" you can run go2cs project to convert Go code, then run converted code.

Installation

There's no official release yet, but you can compile the code to produce a go2cs executable.

Copy the go2cs.exe into the %GOBIN% or %GOPATH%\bin path. This should compile as a standalone executable for your target platform with no external dependencies, see publish profiles.

Usage

  1. Make sure source application already compiles with Go (e.g., go build) before starting conversion. That means any needed dependencies should already be downloaded and available, e.g., with go get.

  2. Execute go2cs specifying the Go source path or specific file name to convert. For example:

  • Convert a single Go file: go2cs -l Main.go
  • Convert a Go project: go2cs MyProject
  • Convert Go Standard Library: go2cs -s -r C:\\Go\src\\

Command Line Options

Option Description
-l (Default: false) Set to only convert local files in source path. Default is to recursively convert all encountered "import" packages.
-o (Default: false) Set to overwrite, i.e., reconvert, any existing local converted files.
-i (Default: false) Set to overwrite, i.e., reconvert, any existing files from imported packages.
-h (Default: false) Set to exclude header conversion comments which include original source file path and conversion time.
-t (Default: false) Set to show syntax tree of parsed source file.
-e (Default: $.^) Regular expression to exclude certain files from conversion, e.g., "^.+_test\.go$". Defaults to exclude none.
-s (Default: false) Set to convert needed packages from Go standard library files found in "%GOROOT%\src".
-r (Default: false) Set to recursively convert source files in subdirectories when a Go source path is specified.
-m (Default: false) Set to force update of pre-scan metadata.
-g (Default: %GOPATH%\src\go2cs) Target path for converted Go standard library source files.
‑‑help Display this help screen.
‑‑version Display version information.
value 0 Required. Go source path or file name to convert.
value 1 Target path for converted files. If not specified, all files (except for Go standard library files) will be converted to source path.

Future Options

A new command line option to prefer explicit types over var would be handy, e.g., specifying -x would request explicit type definitions; otherwise, without applying setting, conversion would default to using var where possible.

If converted code ever gets manually updated, e.g., where a new import is added, a command line option that would "rescan" the imports in a project and augment the project file to make sure all the needed imports are referenced could be handy.

C# to Go?

If you were looking to "go" in the other direction, a full code based conversion from C# to Go is currently not an option. Even for the most simple projects, automating the conversion would end up being a herculean task with so many restrictions that it would likely not be worth the effort. However, for using compiled .NET code from within your Go applications you have options:

  1. For newer .NET Core applications, I would suggest trying the following project from Matias Insaurralde: https://github.com/matiasinsaurralde/go-dotnet -- this codes uses the CLR Hosting API which allows you to directly use .NET based functions from within your Go applications.

  2. For traditional .NET applications, a similar option would be to use cgo to fully self-host Mono in your Go application, see: http://www.mono-project.com/docs/advanced/embedding/.

Background

For more background information, see here.

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.7%
  • Other 1.3%