![go2cs](images/go2cs-small.png)
# Golang to C# Converter
Converts source code developed using the Go programming language (see [Go Language Specification](https://golang.org/ref/spec)) to the C# programming language (see [C# Language Specification](https://github.com/dotnet/csharplang/blob/master/spec/README.md)).
![CodeQL](https://github.com/GridProtectionAlliance/go2cs/workflows/CodeQL/badge.svg)
## News
* Recent example usages of `go2cs` allow the use of [Golang](https://golang.org/ref/spec) as the scripting language for the [Unity](https://unity.com/) and [Godot](https://godotengine.org/) game engine platforms. See the [GoUnity](https://github.com/ritchiecarroll/GoUnity) and [GodotGo](https://github.com/ritchiecarroll/GodotGo) projects.
* Project has been updated to use .NET 5.0 and supports [publishing as a self-contained executable](https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained).
## 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 behaviorally and visually similar C# code -- see [conversion strategies](ConversionStrategies.md).
* Code conversions focus first on making sure C# code runs as behaviorally similar to Go code as possible. This means, for now, leaving out things like code conversions into `async` functions. Instead conversions make things operate the way they do in Go, e.g., simply running a function on the current thread or running it in on the thread pool when using a [`goroutine`](https://golang.org/ref/spec#Go_statements).
* C# conversions attempt to make code visually similar to original Go code to make it easier to identity corresponding functionality. As Go is a minimalist language, it provides high-level functionality provided by the compiler, often much more than C# does. As such, converted C# code will have more visible code than Go for equivalent functionality, however much 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](https://docs.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke) 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
### Automated Code Conversion of Go Standard Library
A few initial conversions of the full Go source code have been completed, you can find the latest results in the repo:
[`src/go-src-converted`](https://github.com/GridProtectionAlliance/go2cs/tree/master/src/go-src-converted). These conversion successes are notable milestones in that they represent full successful conversions of the entire Go source library using the [ANTLR4 Golang grammar](https://github.com/antlr/grammars-v4/tree/master/golang) without failing. Each iteration improves on the next, here are few examples:
* [errors/errors.go](https://github.com/golang/go/blob/master/src/errors/errors.go) => [errors/errors.cs](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/go-src-converted/errors/errors.cs)
* [fmt/format.go](https://github.com/golang/go/blob/master/src/fmt/format.go) => [fmt/format.cs](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/go-src-converted/fmt/format.cs)
* [compress/gzip/gunzip.go](https://github.com/golang/go/blob/master/src/compress/gzip/gunzip.go) => [compress/gzip/gunzip.cs](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/go-src-converted/compress/gzip/gunzip.cs)
Not all converted standard library code will compile yet in C# yet - work remaining to _properly_ parse and convert all Go source library files, with its many use cases and edge cases, can be inferred by examining the warnings in the [`build log`](https://github.com/GridProtectionAlliance/go2cs/tree/master/src/go-src-converted/build.log) for this initial conversion. This log should help lay out a road map of remaining tasks.
Note that go2cs simple conversions currently depend on a small subset of the Go source library, [`src/gocore`](https://github.com/GridProtectionAlliance/go2cs/tree/master/src/gocore), that was manually converted. As the project progresses, there will be a merger of automatically converted code and manually converted code. For example, the [`builtin`](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/gocore/golib/builtin.cs) Go library functions will always require some special attention since many of its features are implemented outside normal Go code, such as with assembly routines.
A strategy to automate conversion of native system calls in Go code, i.e., a function declaration without a body that provides a signature for a native external function, is to create a [`partial method`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/partial-method) in C# for the native call. A manually created file that implements the partial method can now be added that will exist along side the auto-converted files and not be overwritten during conversion.
### Recent Activity
Converted code now targets .NET 5.0 and C# 9.0.
Currently, work to improve code conversions is progressing by walking through each of the [behavioral testing](https://github.com/GridProtectionAlliance/go2cs/tree/master/src/Tests/Behavioral) projects. Iterating through each of these simple use cases improves overall automated code conversion quality.
Sets of common Go sample code have been manually converted to C# using the current [C# Go Library](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/gocore/). As an example, all relevant code samples from the "[Tour of Go](https://tour.golang.org/welcome/1)" have been converted to C#, [see converted code](https://github.com/GridProtectionAlliance/go2cs/blob/master/src/Examples/Manual%20Tour%20of%20Go%20Conversions/). Ultimately would like to see this in head-to-head mode using [Try .NET](https://github.com/dotnet/try), for example:
![go2cs](images/HeadToHead-Small.png)
Currently converted code will not execute with latest release of Try .NET (see [posted issue](https://github.com/dotnet/try/issues/859)). 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](https://www.nuget.org/packages?q=%22package+in+.NET+for+use+with+go2cs%22).
## Testing
Before attempting conversions it is important that Go coding environment is already properly setup, especially `GOPATH` environmental variable. See [Getting Started](https://golang.org/doc/install) 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:
```Shell
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:*
没有合适的资源?快使用搜索试试~ 我知道了~
go2cs:Golang转C#转换器
共2000个文件
cs:12884个
metadata:540个
csproj:128个
需积分: 46 15 下载量 184 浏览量
2021-05-06
05:22:41
上传
评论 2
收藏 29.19MB ZIP 举报
温馨提示
Golang转C#转换器 将使用Go编程语言(请参阅)开发的源代码转换为C#编程语言(请参阅)。 消息 go2cs最新示例用法允许将用作和游戏引擎平台的脚本语言。 请参阅和项目。 项目已更新为使用.NET 5.0,并支持。 目标 将Go代码转换为C#,以便可以在.NET生态系统中直接使用Go代码。 这是go2cs的主要目标。 将Go代码转换为行为和外观相似的C#代码-请参阅。 代码转换首先关注确保C#代码在行为上尽可能类似于Go代码。 到目前为止,这意味着省去了将代码转换为async函数之类的事情。 相反,转换使事物按其在Go中的方式进行操作,例如,仅在当前线程上运行一个函数,或者在使用时在线程池上运行它。 C#转换试图使代码在视觉上类似于原始的Go代码,以使其更容易标识相应的功能。 由于Go是一种极简语言,它提供了编译器提供的高级功能,通常比C#所提供的功能更多。 这样,对于同等
资源详情
资源评论
资源推荐
收起资源包目录
go2cs:Golang转C#转换器 (2000个子文件)
deploy-gocore.bat 354B
convert-gosrc.bat 287B
CompileGrammar.bat 128B
clear-metadata.bat 33B
CNAME 9B
packages.config 224B
App.config 184B
App.config 184B
App.config 180B
zipdata.cs 2.39MB
rewriteAMD64.cs 1.66MB
rewriteARM64.cs 1.39MB
rewritegeneric.cs 1.34MB
rewriteS390X.cs 1.06MB
rewriteARM.cs 993KB
rewritePPC64.cs 810KB
zerrors_windows.cs 625KB
rewrite386.cs 604KB
h2_bundle.cs 525KB
opGen.cs 516KB
tables.cs 382KB
ssa.cs 365KB
rewriteMIPS64.cs 362KB
rewriteMIPS.cs 347KB
root_darwin_ios.cs 344KB
tables12.0.0.cs 344KB
tables11.0.0.cs 344KB
tables10.0.0.cs 342KB
tables9.0.0.cs 340KB
tables12.00.cs 271KB
tables12.00.cs 271KB
tables11.0.0.cs 269KB
tables11.0.0.cs 269KB
tables10.0.0.cs 266KB
tables10.0.0.cs 266KB
tables9.0.0.cs 263KB
tables9.0.0.cs 263KB
proc.cs 254KB
GoParser.cs 253KB
zsyscall_windows.cs 251KB
rewriteRISCV64.cs 237KB
tables.cs 232KB
rewriteWasm.cs 210KB
gcc.cs 193KB
walk.cs 183KB
tables.cs 181KB
regalloc.cs 173KB
exec.cs 171KB
typecheck.cs 170KB
zerrors_linux.cs 167KB
server.cs 165KB
decode.cs 159KB
tables.cs 154KB
loader.cs 154KB
d3.cs 154KB
type.cs 151KB
sql.cs 150KB
lib.cs 147KB
data.cs 146KB
alldocs.cs 145KB
x509.cs 141KB
transport.cs 136KB
lib.cs 135KB
data.cs 135KB
value.cs 131KB
builder.cs 128KB
tables12.0.0.cs 128KB
message.cs 127KB
message.cs 127KB
ast.cs 126KB
pkg.cs 125KB
demangle.cs 125KB
tables11.0.0.cs 125KB
zerrors_linux_s390x.cs 120KB
ztypes_linux.cs 119KB
tables10.0.0.cs 119KB
zerrors_freebsd_arm64.cs 118KB
zerrors_freebsd_386.cs 118KB
zerrors_freebsd_amd64.cs 118KB
tables9.0.0.cs 117KB
zerrors_linux_ppc64le.cs 117KB
parser.cs 116KB
zsyscall_windows.cs 116KB
zerrors_linux_riscv64.cs 115KB
zerrors_linux_ppc64.cs 115KB
elf.cs 114KB
dwarf.cs 114KB
zsyscall_darwin_386.cs 113KB
zsyscall_darwin_amd64.cs 112KB
zerrors_linux_mipsle.cs 112KB
zerrors_linux_mips.cs 112KB
dwarf.cs 112KB
zsyscall_darwin_arm.cs 112KB
elf.cs 112KB
mbitmap.cs 112KB
zerrors_linux_arm64.cs 112KB
zsyscall_darwin_arm64.cs 112KB
zerrors_linux_mips64le.cs 111KB
zerrors_linux_mips64.cs 111KB
zerrors_freebsd_arm.cs 110KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
在南极找不到南
- 粉丝: 26
- 资源: 4605
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0