A deep dive into porting Go's io package to C, covering the key design challenges: representing Go slices as C structs, handling multiple return values via result structs and unions, modeling sentinel errors as pointer-comparable structs, and implementing Go interfaces using fat structs with function pointers. The post also covers type assertion via function pointer comparison and uses alloca for stack-allocated buffers in the Copy function. The result is verbose but structurally faithful C code that mirrors Go's io.Reader, io.Writer, LimitedReader, and Copy semantics.

12m read timeFrom antonz.org
Post cover image
Table of contents
The io packageSlicesMultiple returnsErrorsInterfacesType assertionSpecialized readersCopyWrapping up

Sort: