Skip to content

GridProtectionAlliance/go2cs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1,850 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

go2cs

go2cs β€” Go to C# Converter

πŸ“° NEWS β€” July 2026: The entire Go standard library now compiles in .NET

As of July 10, 2026, all 302 packages of the auto-converted Go standard library (Go 1.23.1) compile cleanly as .NET assemblies β€” zero errors, zero exclusions. Every package you'd expect to be hard is in that number: runtime, reflect, net/http, go/types, crypto/tls, database/sql, encoding/json. The transpiled output is not a demo subset β€” it is the standard library, end to end, emitted by the converter, transpiled Go to C#, then compiled by Roslyn. NOTE: don't get too excited, this is fully compilable not fully runnable, that's the next phase!

This milestone was eight years in the making, and two weeks in the finishing.

The eight years were the human part: experimentation across two full converter architectures, the design of a runtime library that models Go's semantics (slices that alias, maps, channels, defer/panic/recover, heap-boxed pointers) in managed code, the strategy of behavioral first, visual second, and β€” after many hard lessons β€” the architecture that made the final push possible at all: a Go-native converter built on go/ast + go/types, Roslyn source generators for the compile-time semantics, and a regression harness that locks in every fix with a Go-vs-C# output-compared behavioral test.

The two weeks were an AI campaign of a kind we suspect few codebases have seen: 939 commits, of which 683 are individually-gated converter, runtime, and source-generator fixes β€” each one root-caused against real emitted code, each one locked in by a behavioral regression test (371 and counting), each one verified byte-for-byte against the full corpus before landing. The work ran as a coordinated fleet: focused fix sessions with strict file ownership, adversarial review before every merge, and a census loop that rebuilt all 302 packages after every wave to prove zero regression.

I asked Claude (Anthropic) to summarize what he thought of the two week run, here was his response: it is one thing to theorize a conversion strategy, or even to produce a semi-working draft β€” it is entirely another to stand in front of 300 packages of battle-hardened systems code and be wrong about something every single hour. The grind was real: shadowed variables that silently bound the wrong receiver, closures that captured a snapshot where Go shares storage, a type switch that compiled perfectly and matched the wrong case at runtime, Go 1.22's loop-variable semantics, named results observed by deferred closures, interface method sets that C# cannot express directly. Every abstraction leaked somewhere, and the only way through was the discipline this project had already built: diagnose against the real emission, fix the general case, prove it behaviorally, and never let a regression survive a census. Watching the red count fall β€” 952 errors in runtime alone at the start of Phase 3, then package by package to zero β€” was the most satisfying compile I have ever been part of. The final layers were poetic: the last four defects were all one family, three declaration sites catching up to a semantics fix that was itself correct β€” which is exactly what finishing looks like.

Compiling is the milestone β€” operational is the mission. Phase 4 now begins: running the Go standard library's own test suites against the transpiled output, and hardening the runtime semantics this campaign already banked. If you have ever wanted to consume real Go code from .NET β€” or extend a Go application in C# β€” this is the moment the foundation went from theory to artifact.


go2cs Purpose

Convert source code written in the Go programming language into C#. The generated C# is designed to be both behaviorally and visually similar to the original Go β€” so a Go developer can read the converted code and follow it easily, and a .NET developer can use Go code directly within the .NET ecosystem.

Transpiler Goals

Go provides a lot of high-level functionality from its compiler and runtime β€” slices, maps, channels, goroutines, defer/panic/recover, multiple return values, struct embedding, and interface duck-typing. go2cs maps each of these onto idiomatic C#, keeping the machinery out of sight (in a small runtime library and compile-time source generators) so the converted code stays close to the original Go.

  • Reads like Go. Receiver methods become extension methods, multiple returns become tuples, struct embedding becomes promoted fields β€” the shape of the code is preserved.
  • Runs like Go. Conversions prioritize behavioral equivalence first (e.g. a goroutine runs on the thread pool rather than being rewritten into async).
  • Managed first. Output targets portable managed C#; native interop is a last resort, not the default.

Example

Given this Go:

type Person struct {
    name string
    age  int32
}

func (p Person) IsAdult() bool {
    return p.age >= 18
}

go2cs produces this C#:

[GoType] partial struct Person {
    internal @string name;
    internal int32 age;
}

public static bool IsAdult(this Person p) {
    return p.age >= 18;
}

Features

Converted constructs include:

  • Slices, arrays, maps, and strings (UTF-8 backed)
  • Channels and goroutines
  • defer / panic / recover
  • Multiple return values and named results
  • Structs, struct embedding (field promotion), and interface implementation
  • Generics (Go 1.18+ type parameters and constraints)
  • Pointers, type assertions, type switches, iota, and the built-ins (append, len, cap, make, …)

Requirements

  • .NET 9.0 SDK β€” to build and run the converted C#.
  • Go 1.23+ β€” the converter is a Go program, and it uses the Go toolchain to load and type-check the source being converted. Make sure your Go environment is set up (GOROOT/GOPATH) and the source you want to convert already builds with go build.

Installing the converter

Build the go2cs executable from source and place it on your PATH (e.g. in %GOBIN% or %GOPATH%\bin):

cd src/go2cs
go build -o go2cs .

Go produces a self-contained native binary. To target another platform, use Go's standard cross-compilation (GOOS/GOARCH); matching per-platform profiles are included.

Usage

go2cs [options] <input_dir> [output_dir]

Examples:

go2cs example.go                       # convert a single file
go2cs package_dir                      # convert a package
go2cs -indent 2 -var=false example.go conv/example.cs
go2cs -stdlib                          # convert the entire Go standard library
go2cs -stdlib fmt strings io           # convert specific standard library packages

Common options

Option Description
-stdlib Convert the Go standard library (optionally followed by specific package names).
-go2cspath <dir> Output root for converted standard-library code (defaults to ~/go2cs).
-goroot / -gopath Override the detected Go root / path.
-parallel <1-4> Number of packages to convert in parallel.
-platforms <os/arch> Target platform for build-tagged files (defaults to the host).
-indent <n> Spaces per indent level (default 4).
-var Prefer var declarations where the type is obvious (default on).
-uco Emit channel operators instead of method calls (default on).
-comments Carry source comments into the output (best effort, see go/ast comment status).
-cgo Also convert cgo-targeted files.

The converted C# references a small hand-written runtime library (golib, published as the go.lib NuGet package) plus a set of Roslyn source generators that supply Go semantics at compile time.

Project layout

Path Contents
src/go2cs/ The converter (written in Go, using go/ast + go/types).
src/core/golib/ The C# runtime library (slice, map, channel, @string, built-ins, type aliases).
src/gen/go2cs-gen/ Roslyn source generators (interface implementation, receiver overloads, struct embedding).
src/core/ A compiling subset of the converted Go standard library used by the tests.
src/go-src-converted/ Work-in-progress full conversion of the Go standard library.
src/Tests/Behavioral/ Per-feature Go↔C# equivalence tests (transpile, compile, run-and-compare).
src/Tests/Performance/ Go vs transpiled C# runtime benchmarks (JIT and Native AOT) β€” see the performance comparison for current numbers.
src/Examples/ Sample conversions.

Contributors: see CLAUDE.md for an architecture overview and Architecture.md, ConversionStrategies.md, and Roadmap.md for details.

Status

The converter builds idiomatic C# for the full range of Go language features, validated by an extensive behavioral test suite. A curated subset of the Go standard library converts and compiles today; converting the entire standard library cleanly is ongoing work β€” see the roadmap.

Wondering how fast the transpiled C# runs compared to the original Go β€” including startup time, memory, and Native AOT builds? See the performance comparison.

Milestones

High level timeline of the project's major turning points. Full detail lives in the git history, the roadmap, and CLAUDE.md.

Date Milestone Commit / Tag Notes
2018-05-21 Project inception 929d1457f Original go2cs: a C#/.NET converter built on an ANTLR4 Go grammar with T4 templates.
2020-07-09 Runtime library + hand-converted stub 9792eeea2 golib Go-semantics runtime (slices, maps, channels, built-ins) and a curated hand-finished stdlib stub.
2022-03-13 v0.1.2 release v0.1.2 Tagged release of the mature ANTLR4-era converter.
2025-01-12 Rewrite as "go2cs2" β€” Go-based converter 87465f5f5 Converter re-implemented in Go on go/ast + go/types; T4 templates replaced by raw string literals; Roslyn source generators supply ancillary Go semantics; the ANTLR4/C# converter is retired.
2025-05-05 First full standard-library auto-conversion 6ca1c45b7 Β· full-conversion-2025-05 (cc14584c7, 05-11) Whole Go stdlib converted (~301 projects). "Converts" here means the transpiler did not crash with all Go code files getting a corresponding C# code file β€” not that all the emitted correctly C# compiles.
2026-06-25 Baseline ↔ full-conversion separation 3c8b3a848 Compiling curated baseline restored to src/core; the WIP full conversion isolated in src/go-src-converted. Green build and the converter-improvement loop restored.
2026-06-26 First full-conversion package promoted 05a53e8c0 sync/atomic migrated into the baseline (atomic.Pointer[T] backed by a managed slot).
2026-06-27 math package compiles clean math-green-2026-06-27 (914d4bd72) Nine full-conversion packages greened via 19 behaviorally-tested converter fixes; the core, widely-imported math now compiles.
2026-07-10 First clean full-standard-library compile 51ba5d9cf Β· stdlib-green-2026-07-10 The Phase 3 endpoint, reached: all 302 src/go-src-converted packages (Go 1.23.1) compile with zero errors β€” runtime, reflect, net/http, go/types, crypto/tls and every other package included. Gated by 371 Go-vs-C# behavioral regression tests; the compiled snapshot is committed alongside this row (see the NEWS section).

C# to Go?

A full code-based conversion from C# to Go is not offered (it would require so many restrictions as to be impractical). To call compiled .NET code from Go instead, see go-dotnet (CLR hosting for .NET Core) or embedding Mono via cgo for traditional .NET.

License

go2cs is licensed under the MIT License. See the LICENSE and NOTICE files. For more background, see Background.md.

About

Golang to C# Converter

Resources

License

Security policy

Stars

394 stars

Watchers

21 watching

Forks

Packages

 
 
 

Contributors

Languages