go2cs

go2cs — Go to C# Converter

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.

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:

Requirements

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 its README 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-06-25 → ongoing Phase 3 compile grind (runtime 952 → 138) f3713df61 Iterative, test-locked converter fixes drive the full stdlib toward a clean compile; runtime — the root of the dependency graph — is down from 952 errors to 138.
pending First clean full-standard-library compile (tag TBD) The Phase 3 endpoint: every src/go-src-converted package compiles. Not yet reached — this row will be filled in when the milestone lands.

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.