Limitations: where the seams are

So you’re about to point go2cs at your own Go code. Before you spend an evening on it, here is where it will fail, where it will fight you, and where it will quietly do something a little different from Go – better you hear it from me than from a stack trace. Specific bugs with workarounds live in Known issues. This page is the limits that come with running Go on .NET, plus the rough spots real code hits today.

This page describes go2cs 1.26.11.2, the current release. Where an earlier release differs, it says so.

The short version

Good odds tonight: pure Go that builds with the Go release go2cs tracks and CGO_ENABLED=0, leans mostly on the standard library, and runs on Windows, Linux or macOS. A longer evening: lots of third-party modules, unsafe, assembly fast paths, cgo, or tests that pull in testify.

What won’t make the trip

What converts, but doesn’t behave exactly like Go

cgo

What converts. Without -cgo, a package whose build selects a file with import "C" stops the run with error GO2CS2001, which names the file. With -cgo, on a machine with a C compiler (go env CC), a package converts when every C use it makes is one go2cs binds. In go2cs 1.26.11.1, that is a call of a C function a shared library defines, passing integers, floating-point numbers and pointers, structs of integers and pointers by value, one struct or one scalar by pointer, or a void * that is nil or points at C memory. C.CString, C.CBytes, C.GoString, C.GoStringN, C.GoBytes, C.malloc, C.free and defer C.f(…) convert, with the package’s C types and constants. A program that uses the converted package needs only the C library.

Since 1.26.11.2, a package also converts when it calls C that its preamble or its own C or assembly files define (go2cs compiles those into a side library beside the bindings), or passes a void * that points into Go memory holding no Go pointers (held still for the call). It reads and writes a C variable of a scalar or struct type in place, and C can call back into an //export through a function pointer Go handed it. A struct with a void * member converts by pointer, the member nil or C memory, and a C pointer typedef (typedef const char *T) converts as a result and as a parameter.

Since 1.26.11.2, Go code that walks C memory converts too: it reads and writes a pointer field of a C struct that lives in C memory (l.next), and loads and stores through a **C.T (*p, as a walk down a C list such as addrinfo’s ai_next does). A pointer field reached through a nested struct value, an array of pointers, a field whose type is a C pointer typedef, and a union are still refused by name, and so is storing a pointer into Go memory into C memory. In go2cs 1.26.11.1, reading a pointer field of a C struct in C memory fails with a panic.

Since 1.26.11.2, a slice of C memory whose element is a C struct with a pointer field (from unsafe.Slice, or (*[N]C.T)(p)[:]) converts as in Go: an element’s fields are read and written through the index, a pointer field included, an element read by value is a copy decoded out of C memory, and a slice of an array member is a window on the C bytes. Storing a whole element by value (s[i] = v, or a range statement that assigns s[i]), storing into an element of an array member (s[i].a[j] = x), and copy into, clear of or an append within the capacity of such a slice are refused by name. Through a pointer to an element (p := &s[i]) the fields and the whole element read as through the index, but an element of an array member (p.a[j]) is refused by name: read it as s[i].a[j]. Handing such a pointer to a function that reads through it (f(p) with a *C.T parameter) and reading a field through a written dereference ((*p).f) are refused by name: read the element as *p or the field as p.f where the pointer is. A slice of an array member of a nested struct (s[i].in.a[:]) is refused by name.

Since 1.26.11.2, a number made from a Go pointer (unsafe.Pointer(uintptr(unsafe.Pointer(&x)) + off), in one expression or through a variable) is handed to C as a void * when it points into Go memory that holds no Go pointers, and that memory is held still for the call. A number one past the end of that memory, the end of a begin/end pair, is not held still, so C may only compare it, never read or write through it. A 64-bit integer argument that is go2cs’s stand-in for the address of a Go value holding Go pointers is refused by name.

Since 1.26.11.2, C may keep a Go pointer past the call when a runtime.Pinner pins the memory, as cgo requires: the pin holds the value and every array inside it still until Unpin. A pinned value whose Go layout is not its C layout (a struct holding an array, for instance) is refused by name. Two cases are not covered:

One target. A -cgo conversion is for one operating system and architecture: the machine’s own, or another one named with -platforms when CC names that target’s C compiler (for example zig cc -target <triple>). The converted package refuses its C bindings on any other: when the package loads, before any of its code runs. A single-package conversion builds against a go2cs clone (dotnet build -p:go2csPath=<go2cs>/src/). Under -recurse -cgo a module’s cgo packages convert with the module, and under -recurse=nuget -cgo they reference the go.* packages. A module with a cgo package packs for publishing as a package for each of win-x64, linux-x64, osx-x64, linux-arm64 and osx-arm64, each with its own C binding. Such a package is published only against a go.lib that carries the load-time target check, which the go.* 1.26.11.1 packages do not. The standard library itself is converted with CGO_ENABLED=0, and a module converted with -recurse -cgo uses it as it is. So a package that imports net or os/user gets their builds without cgo, the ones Go itself uses with CGO_ENABLED=0: net resolves host names with Go’s own resolver, not the C library’s, and os/user reads the user database files directly. go2cs does not convert the net resolver with cgo yet (Roadmap). A package that imports runtime/cgo or plugin is refused by name: without cgo they are not working builds.

That is go2cs 1.26.11.2. go2cs 1.26.11.1 converts one cgo package at a time, for the machine’s own operating system and architecture only: there -cgo does not combine with -recurse, a cgo package cannot be packed, and the target check runs at the package’s first C call rather than when it loads.

What is refused, by name. Each of these is not supported yet; the Roadmap says what is planned.

Where it differs from Go. For a pointer to one whole Go struct or scalar, in a variable or a struct field, go2cs hands C a copy of the value it points at, and copies it back after the call. A pointer to a scalar element of a Go array or slice, a typed scalar view over Go storage, and memory pinned with runtime.Pinner cross in place instead, held still for the call (an element of a Go array of C structs is copied with its whole array, as below). So cgo’s pointer check differs from Go’s in five cases, all where a program breaks or bends cgo’s pointer rules. In two, Go panics and go2cs proceeds:

Go checks a pointer held in a variable against the whole object it points into. go2cs proceeds in both cases, since C only sees the copy (or, for a scalar element of a Go array, the array’s own memory, which holds no Go pointer). This variable check is the one most cgo calls in the Go toolchain’s own cgo packages take.

In three, Go proceeds and go2cs refuses the call when it is made:

A Go struct viewed as a C struct crosses as an image, and C’s writes land when the call returns. A struct pointer that views a Go struct of another type which holds an array (net’s (*C.struct_sockaddr)(unsafe.Pointer(&sa)), sa a syscall.RawSockaddrInet4) has no storage go2cs can hand C, so go2cs hands C an image of the whole Go struct, at Go’s own offsets, and decodes it back after the call. It decodes only when C changed the image, so C’s write lands in the Go struct when the call returns, not while C runs:

Three views of one Go value in one call share one image, as Go’s one memory does. One Go value handed to C both as such a view and through another pointer of the same call is refused by name, and so is a view of a Go struct that holds a Go pointer.

A known memory-safety gap: a pointer rebuilt from a number, in three narrow cases. A number made from a Go pointer (u := uintptr(unsafe.Pointer(p)), then unsafe.Pointer(u + n), or Go’s one-expression form) and handed to C as a void * crosses, since 1.26.11.2, as that Go memory, held still for the call, while go2cs can tell which Go value it came from. In go2cs 1.26.11.1 such a number is refused by name. In both, three cases cross as C memory instead, because go2cs is no longer holding the memory, or never was. In each, the call goes ahead, and the memory may move while C uses it:

A Go pointer in a void * member of one struct is refused with Go’s text for the call’s form. For C.f(&x) Go names the member’s kind, “Go pointer to unpinned Go unsafe pointer”; through a variable holding the struct’s address (p := &x; C.f(p)) Go checks the whole object and says “Go pointer to unpinned Go pointer”. go2cs says the same in each, from the call form the conversion passes the wrapper.

A Go array of C pointers costs its whole length on every call. C.f(&argv[0]) lets C read or fill every pointer in the array, as in Go. go2cs holds such an array as Go pointers rather than as machine words, so for the call it copies every element of the array into a word array, hands C that, and copies every word C changed back afterwards. The copy covers the whole array behind the slice, not only the part C uses: argv[2:] of a long array copies the long array, on every call. A word C writes that points into the Go memory the call handed it, the array itself included (argv[0] = &argv[2]), is refused by name, where Go would keep it. An array of C pointers that lives in C memory crosses by its own address, with no copy.

A Go array or slice of C structs costs its whole length on every call. C.f(&cols[0]) lets C read or write every struct in the array, before the one it is handed included, as in Go. go2cs holds such structs in a layout C cannot read, so for the call it copies every element of the backing array into an array of C structs, hands C the element’s copy, and after the call copies back each element whose bytes C changed. So a goroutine’s write to an element C did not touch survives the call. The copy covers the whole backing array, from its first element, whatever part C uses. Where go2cs differs from Go:

Platforms. A converted cgo program was built and run against the Go build of the same program on linux/amd64, darwin/amd64, darwin/arm64 and windows/amd64, and printed what Go printed apart from the differences above.

What works, but costs you something

Third-party modules: where they trip today

The validated modules page lists the modules validated so far.

What “validated” means, and what it doesn’t

A package’s own Go Test functions were converted, run, and compared with go test verdict for verdict, by full test name. That’s a real bar, and narrower than the word sounds:

Platforms and toolchain

Reading and debugging the output

What you might expect to break, and doesn’t

The language itself (generics, closures, struct embedding, range-over-func, typed nil pointers in interfaces, randomized map order), defer/panic/recover, channels and select, reflect (encoding/json and text/template pass their own suites on it, on Windows, Linux and macOS), and net/http with HTTP/2 and crypto/tls over real sockets.

When you hit a seam

  1. Start from a clean Go build: GOTOOLCHAIN=local, then go build ./... for the same OS and tags.
  2. Read the conversion’s warnings and its summary line, not the exit code.
  3. A clean build that throws NotImplementedException wants a pure-Go build tag. A C# compile error in converted code is a converter bug until proven otherwise.
  4. A behavior difference in the standard library? Check the package’s disclosures on Validated Test Packages.
  5. Check Known issues, then open an issue with the output of go2cs -version, your OS, the error text, and the smallest Go that reproduces it.

If you find a seam that isn’t on this page, that’s the one I most want to hear about.