Skip to content

idea: Go bindings **Native** support without CGO #4848

@yuchanns

Description

@yuchanns

Hello community!

I'd like to introduce you to a native method for supporting Go bindings without CGO.

This capability primarily relies on two libraries: purego and libffi.

  • purego: A library for calling C functions from Go without Cgo, but it lacks support for passing and returning structure values.
  • libffi: A portable foreign-function interface library. It is also a dependency of Python and Ruby for their FFI functionality.

To support the passing and returning of structure values, we need to create lightweight Go libffi bindings by wrapping purego, which is already provided by ffi. We can either use it or maintain it ourselves directly.

Then we can call arbitrary C functions using libffi from Go without enabling CGO, which is why I refer to it as Native support!

Here's a POC that demonstrates its functionality with backend memory and aliyun_drive: gopendal.

Pros:

  • No need to enable CGO. - It is beneficial to promote opendal to various well-known pure Go basic libraries.
  • Dynamic injection of libopendal_c.so - Users can inject custom featured libs under their control.

Cons:

  • Still requires libopendal_c.so. - We can embed it in go files for each platform_suffix file. eg: *_linux.go, *_windows.go,
  • Depends on preinstalled libffi. - libffi is widely used in many languages and commonly preinstalled on many platforms. You can easily install it with the package manager if not available.
  • Currently only supports Linux and BSD. - This limitation arises from the go wrapper ffi. Ideally, it should be able to support platforms where libffi and purego intersect, meaning Windows, Linux, and MacOS should all be supported.
  • Performance has not been tested yet. But I don't think this will be a concern as CGO is the major problem.

What do you think? I look forward to your comments!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions