Skip to content

cmd/cgo: Allow C code to use structures defined in Go #8114

@gopherbot

Description

@gopherbot

by fuzxxl:

There are some C structures that cannot be correctly accessed from Go code as Go does
not provide the features needed to access them. Examples are unions, bitfields and
structures that are affected by issue #7560. Therefore, C code is needed to unmarshall
and marshall these structures into Go code.

Sadly, this is very uggly to do right now as there seems to be no sane way of having a C
function operate on types defined in Go code. Right now, I have to define the structure
I'm marshalling into on the C side of code which requires me to marshall the already
marshalled structure again if I want to provide that structure as an interface to other
packages.

Ideally, something like this is possible:

package example

/*
#include "go.h"

// struct I want to marshall
struct something {
    // ...
}

void marshall_something(struct something *s, GoSomething *g) {
    // unmarshall s into g
}
*/
import "C"

//export
type something struct {
    // corresponding Go type that is visible to C
    // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions