有什么方法可以在Go中动态解析C结构吗?

I defined a struct in C like:

struct SomeStruct
{
    int var1;
    bool var2;
    double var3;
    int var4[10];
    int var5[10][10];
}
struct SomeStruct entity;

And somewhere there was a input box that some user input in GO:

func("entity.var3")

It will return value of entity.var3 in C struct.

Actually I could already implement it in python by cffi and:

def get_one_variable(buffer, setup):
    value = buffer
    for level in setup:
        if isinstance(level, str):
            value = getattr(value, level)
        else:
            [base, extends] = level
            value = getattr(value, base)
            for extend in extends:
                value = value[extend]
    return value

Where buffer is python cffi data pointer defined with "FFI.cdef" and setup resolved by:

def parse_variable(self, line):
    line = line.replace('
', '').replace(' ', '')
    split = line.split('.')
    variable = []
    for child in split:
        match = self.BASE_EXT_REGEX.match(child)
        if match is None:
            variable.append(child)
        else:
            base_name = match.group('base_name')
            ext_name = match.group('ext_name')
            variable.append([base_name, [int(index) for index in
                                         ext_name.replace('[', ']').replace(']]', ']').strip(']').split(']')]])
    return variable

So I can dynamically resolve "entity.var1", "entity.var2", "entity.var3", "entity.var4[0]", "entity.var5[0][1]".

Is there something or someway similar in GO?

This is handled by CGO which is a special package in Go that allows for easy C integration. You can read more about it here and here. Given your examples, a simple CGO example would be:

/*
struct SomeStruct
{
    int var1;
    bool var2;
    double var3;
    int var4[10];
    int var5[10][10];
}
*/
import "C"
import "fmt"

func main(){
    thing := C.struct_SomeStruct{}
    thing.var1 = C.int(5)
    fmt.Printf("My Struct's var field %d
",int(thing.var1))
}