github.com/notti/go-dynamic@v0.0.0-20190619201224-fc443047424c/doc.go (about)

     1  /*
     2  Package nocgo provides a dlopen wrapper that doesn't need cgo.
     3  
     4  WARNING! This also supports cgo! So if you want to ensure no cgo, you have to set the environment variable CGO_ENABLED=0 for the build process.
     5  
     6  Usage
     7  
     8  Don't use this in production! This is meant as a PoC for golang and subject to changes.
     9  
    10  So far only floats, integers, and pointers are supported (No structs, no complex types, and no callbacks, but that wouldn't be hard to implement).
    11  See example, examplelibpcap, and the documentation below for examples.
    12  
    13  Go to C type mappings
    14  
    15  In the type specification struct go types, which will be mapped to C types, have to be used.
    16  
    17  	Go type                                    C Type
    18  	=========================================================
    19  	int8, byte                                 char
    20  	uint8, bool                                unsigned char
    21  	int16                                      short
    22  	uint16                                     unsigned short
    23  	int32                                      int
    24  	uint32                                     unsigned int
    25  	int64                                      long
    26  	uint64                                     unsigned long
    27  	float32                                    float
    28  	float64                                    double
    29  	[], uintptr, reflect.UnsafePointer, *      *
    30  
    31  The last line means that slices and pointers are mapped to pointers in C. Pointers to structs are possible, but the structs must follow C alignment rules!
    32  Be carefull with the mappings - there is NO type checking, which is actually impossible since the imported library doesn't know these things.
    33  
    34  go int is not supported to prevent confusion: int sizes in go for 32bit and 64 bit differ, while in C (cdecl) they do not!
    35  
    36  Helperfunctions for converting between C strings and go strings are provided (see below in the function documentation).
    37  
    38  Argument Specifications
    39  
    40  Arguments to functions must be specified via a pointer to func variable. A call to lib.Func will examine arguments and
    41  eventual return value (only one or no return values allowed!), and set the function variable to a wrapper that will call into the desired C-function.
    42  
    43  Example for pcap_open_live (libpcap):
    44  
    45  C declaration:
    46  	pcap_t *pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf);
    47  
    48  nocgo declaration:
    49  	var pcapOpenLive func(device []byte, snaplen int32, promisc int32, toMS int32, errbuf []byte) uintptr
    50  
    51  ret uses uintptr as type since pcap_t* is just passed to every other libpcap function and we don't care or need to know whats actually in there or where it's pointing to.
    52  */
    53  package nocgo