Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

go2cs

> 编程语言
Open source

Golang to C# Converter

396 stars0 likes1 views
WebsiteGitHub

About

Golang to C# Converter

go2cs — Go to C# Converter

Browse all: Go Standard Library NuGet packages


NEWS — The Go 1.23.12 record closes at 97.6% of the implementable set; the corpus hops to Go 1.24

204 of the 215 testable standard-library packages pass their own Go test suites in C# — 28,459 matching verdicts against go test -json, compared verdict for verdict, with 167 divergences disclosed by exact failure signature and nothing else waived. Six of those 215 cannot be validated at all — no eligible tests on this platform, a broken upstream oracle, a suite whose whole subject is the raw memory layout a managed runtime deliberately does not have, or a comparison that runs cleanly and validates nothing — so the honest denominator is 209, putting the roster at 97.6%. Each of the six is listed with its class, mechanism and evidence in the exclusion ledger, and any one of them rejoins the count the day its evidence changes. On Linux, 198 of the 202 applicable rows validate at their own Linux counts. A package appears on the roster only when every eligible test agrees, and every row links a proof page listing Go's verdict beside go2cs's, test by test.

Those figures are the Go 1.23.12 anchor — a closed record rather than a running total. The corpus now moves to Go 1.24.13, and a version hop re-derives every roster row from the new release's own test sources, so the five packages still unbanked here re-validate there on exactly the footing of the 204 that banked. The 1.23.12 corpus ships one final NuGet release first, freezing its roster, its proof pages and every package README at the record above — see the announcement.

➡ All announcements can be found in the go2cs News Archive.

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.

  • Browse transpiled code: Converted Go Standard Library
  • Explore Go and generated C# side by side: Tour of go2cs
  • Learn how it works: Go to C# Conversion Strategies
  • Walk through an example: Converting a real-world module
  • Compile in Visual Studio: Go Standard Library Solution
  • Run converted Go test validation: Try it yourself
  • Track which stdlib test suites pass in C#: Validated Test Packages
  • View example converted test: utf8_test.cs
  • See current project status and milestones

Frequently asked questions

  • Why is a Go to C# transpiler needed? Integration opportunities.
  • Won't converted C# code be slower? Usually — but not always.

Transpiler Goals

Go leans on its compiler and runtime for slices, maps, channels, goroutines, defer/panic/recover, multiple returns, struct embedding and interface duck-typing. go2cs maps each 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:

go
type Person struct {
    name string
    age  int32
}

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

go2cs produces this C#:

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

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

Real standard-library conversions, side by side

The goal — reads like Go — is easiest to judge on real code. Below are converted standard-library files next to their original Go 1.23.12 source, in order of increasing richness:

Package Go 1.23.12 source Converted C# What it shows
errors errors.go errors.cs Error values and an unexported type satisfying the error interface.
cmp cmp.go cmp.cs Generics with an ordered-type constraint.
unicode/utf8 utf8.go utf8.cs Constants keeping Go's hex/binary literal formatting; arrays and structs.
sort search.go search.cs Binary search driven by a func(int) bool closure.
strings reader.go reader.cs A struct with receiver methods, tuple returns, and interface implementation.
container/list list.go list.cs A doubly-linked list — pointers and receiver methods.

Browse the whole set under src/core.

Features

go2cs converts the full Go language surface — the same converter that emits the 302 packages above:

Types & values

  • Slices and arrays (backing-array aliasing preserved, as in Go), maps, and UTF-8-backed @string
  • int / uint as platform-width native integers; named numeric types and untyped-constant semantics
  • Constants and iota, preserving Go's numeric literal formatting (hex, binary, underscores, exponents)
  • Pointers with automatic heap-boxing driven by escape analysis; nil; unsafe.Pointer
  • Type definitions and aliases — including exported aliases that resolve across assembly boundaries

Functions & methods

  • Multiple return values and named results, as tuples
  • Receiver methods as extension methods, with distinct pointer- and value-receiver overloads
  • Function values, closures (honoring Go's shared-storage capture semantics), and variadic functions
  • defer / panic / recover, including named results observed and mutated by deferred closures

Concurrency

  • Goroutines, run on the thread pool (behavioral equivalence first — not rewritten into async)
  • Channels with channel-operator (| Package deadline for a converted-test action, in Go duration syntax (default `2m`). For `run`/`compare` it is handed to **both** sides — `go test -timeout` and the converted host's own `-timeout` — so they agree. A suite that legitimately runs long needs a value above both defaults: `hash/maphash` takes ~15 minutes in C# where Go's takes 7.6 seconds, so it is validated with `-test-timeout 30m`. | | `-go2cspath ` | Runtime/stdlib root (env `GO2CSPATH`; default `~/go2cs`) used by generated `$(go2csPath)…` references, and the output root for `-stdlib`. For a single-package/file conversion, C# output goes to optional `[output_dir]` (in place by default). | | `-goroot` / `-gopath` | Override the detected Go root / path. | | `-platforms ` | Target platform for build-tagged files (defaults to the host). A comma-separated **list** (`windows/amd64,linux/amd64,darwin/amd64`) is accepted and today requires `-platform-census`: a conversion still emits for exactly one target, so a list without the census flag is rejected rather than silently converting the first. | | `-platform-census ` | With `-stdlib` and two or more `-platforms` targets: convert once per target into an isolated, seeded staging root under, compare what each run actually emitted, and write \platform-manifest.json classifying every artifact as shared, variant, partial or platform-exclusive. Produces no converted output of its own — -go2cspath is read as the seed and never written to. | | -tags | Build tags applied when loading packages. -stdlib and -tests apply purego by default; an explicit value replaces it. | | -indent | 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). | | -csproj | Generate project files from a custom .csproj template instead of the embedded one. | | -tree | Print each file's Go parse tree (go/ast) to stdout during conversion — a diagnostic aid. | | -debug | Disable the converter's per-file panic recovery, so a conversion failure crashes with a full stack trace instead of being reported as a warning. | | -cgo | Also convert cgo-targeted files. Not yet functional — but planned, not abandoned: the ratified cgo interop plan lays out the import "C" ladder (P/Invoke-backed, staged after the current validation campaign), and this flag comes alive with it. |

All converted C# code references a 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 (published as go.gen). A -recurse=nuget conversion wires both up for you.

Converting a real-world module

The -recurse option converts a whole downloaded application together with every third-party dependency package in its transitive import closure — in dependency order (least-dependencies-first) — while referencing (not reconverting) the pre-converted standard library. The result is a C# solution you can open and build. With -recurse=nuget the standard library, the golib runtime and the go2cs-gen analyzer come from nuget.org, so nothing has to be staged on the machine beforehand. (Prefer the standard library as local C# source? See building against a local standard library below.)

Wondering which real-world Go packages make good conversions? The go2cs Target Atlas surveys the Go ecosystem's best candidates — the study behind the first operational package conversions now being planned.

Here is the full round-trip for a small CLI that uses github.com/fatih/color, which itself pulls in `github.com/mattn/go-color

Issues· 3 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言