cgo

Access C array of type const char * from Go

我们两清 提交于 2019-12-02 06:05:58
问题 I have a C file with an array of type const char * , lets call it myStringArray[] , something like: const char *myStringArray[] = { "NAME_OF_FIRST_THING", "NAME_OF_SECOND_THING", "NAME_OF_THIRD_THING"} I need Go to index into that C array, using cgo , and convert an array entry into a Go string. The following code compiles but does not work correctly; you can see from the output that follows, it is indexing along the strings rather than up the array: myGoString := C.GoString((*C.char) (unsafe

Passing array of string as parameter from go to C function

天大地大妈咪最大 提交于 2019-12-02 04:25:53
I have one C function: int cgroup_change_cgroup_path(const char * path, pid_t pid, const char *const controllers[]) I want to call it in go language by using cgo. How to pass the third parameter as it accepts a C array of string. You can build the arrays using c helper functions and then use them. Here is a solution to the same problem: // C helper functions: static char**makeCharArray(int size) { return calloc(sizeof(char*), size); } static void setArrayString(char **a, char *s, int n) { a[n] = s; } static void freeCharArray(char **a, int size) { int i; for (i = 0; i < size; i++) free(a[i]);

Access C array of type const char * from Go

感情迁移 提交于 2019-12-02 03:33:45
I have a C file with an array of type const char * , lets call it myStringArray[] , something like: const char *myStringArray[] = { "NAME_OF_FIRST_THING", "NAME_OF_SECOND_THING", "NAME_OF_THIRD_THING"} I need Go to index into that C array, using cgo , and convert an array entry into a Go string. The following code compiles but does not work correctly; you can see from the output that follows, it is indexing along the strings rather than up the array: myGoString := C.GoString((*C.char) (unsafe.Pointer(uintptr(unsafe.Pointer(C.myStringArray)) + uintptr(index) * unsafe.Sizeof(C.myStringArray))))

exec: “gcc”: executable file not found in %PATH% when trying go build

谁都会走 提交于 2019-11-30 10:44:10
问题 I am using Windows 10. When I tried to build Chaincode it reported this error # github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11 exec: "gcc": executable file not found in %PATH% My chaincode imports: import ( "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" pb "github.com/hyperledger/fabric/protos/peer" ) It's running fine in Docker. 回答1: gcc (the GNU Compiler Collection) provides a C compiler. On Windows, install TDM-GCC. The github.com/miekg/pkcs11 package

Why cgo's performance is so slow? is there something wrong with my testing code?

邮差的信 提交于 2019-11-30 02:41:34
I'm doing a test: compare excecution times of cgo and pure Go functions run 100 million times each. The cgo function takes longer time compared to the Golang function, and I am confused with this result. My testing code is: package main import ( "fmt" "time" ) /* #include <stdio.h> #include <stdlib.h> #include <string.h> void show() { } */ // #cgo LDFLAGS: -lstdc++ import "C" //import "fmt" func show() { } func main() { now := time.Now() for i := 0; i < 100000000; i = i + 1 { C.show() } end_time := time.Now() var dur_time time.Duration = end_time.Sub(now) var elapsed_min float64 = dur_time

How to convert [1024]C.char to [1024]byte

杀马特。学长 韩版系。学妹 提交于 2019-11-29 14:21:08
How do I convert this C (array) type: char my_buf[BUF_SIZE]; to this Go (array) type: type buffer [C.BUF_SIZE]byte ? Trying to do an interface conversion gives me this error: cannot convert (*_Cvar_my_buf) (type [1024]C.char) to type [1024]byte The easiest and safest way is to copy it to a slice, not specifically to [1024]byte mySlice := C.GoBytes(unsafe.Pointer(&C.my_buff), C.BUFF_SIZE) To use the memory directly without a copy, you can "cast" it through an unsafe.Pointer . mySlice := (*[1 << 30]byte)(unsafe.Pointer(&C.my_buf))[:int(C.BUFF_SIZE):int(C.BUFF_SIZE)] // or for an array if BUFF

How to pass pointer to slice to C function in go

雨燕双飞 提交于 2019-11-29 11:11:07
Background: using cgo to call C functions from Golang. I want to use a C function which has this signature: int f(int *count, char ***strs) . It will modify the data of count and strs , which is the reason why it uses pointer to them. The value of count is the length of strs ; strs is an array of string; the return value is simply an (boolean) indicator which states whether there is an error or not. In golang, I can successfully pass and modify count by using C.f((*C.int)(&count)) ; pass []string by using []*C.char . Sample code is like this: /* #include <stdio.h> int f(int *c, char **str) {

What does (*[1 << 30]C.YourType) do exactly in CGo?

南笙酒味 提交于 2019-11-29 08:52:38
In the Golang wiki , under "Turning C arrays into Go slices", there is a block of code that demonstrates how to create a Go slice backed by a C array. import "C" import "unsafe" ... var theCArray *C.YourType = C.getTheArray() length := C.getTheArrayLength() slice := (*[1 << 30]C.YourType)(unsafe.Pointer(theCArray))[:length:length] Can anyone explain exactly what (*[1 << 30]C.YourType) does? How does it turn an unsafe.Pointer into a Go slice? JimB *[1 << 30]C.YourType doesn't do anything itself, it's a type. Specifically, it's a pointer to an array of size 1 << 30 , of C.YourType values. What

Why cgo's performance is so slow? is there something wrong with my testing code?

人盡茶涼 提交于 2019-11-28 23:06:58
问题 I'm doing a test: compare excecution times of cgo and pure Go functions run 100 million times each. The cgo function takes longer time compared to the Golang function, and I am confused with this result. My testing code is: package main import ( "fmt" "time" ) /* #include <stdio.h> #include <stdlib.h> #include <string.h> void show() { } */ // #cgo LDFLAGS: -lstdc++ import "C" //import "fmt" func show() { } func main() { now := time.Now() for i := 0; i < 100000000; i = i + 1 { C.show() } end

How to convert [1024]C.char to [1024]byte

妖精的绣舞 提交于 2019-11-28 08:15:46
问题 How do I convert this C (array) type: char my_buf[BUF_SIZE]; to this Go (array) type: type buffer [C.BUF_SIZE]byte ? Trying to do an interface conversion gives me this error: cannot convert (*_Cvar_my_buf) (type [1024]C.char) to type [1024]byte 回答1: The easiest and safest way is to copy it to a slice, not specifically to [1024]byte mySlice := C.GoBytes(unsafe.Pointer(&C.my_buff), C.BUFF_SIZE) To use the memory directly without a copy, you can "cast" it through an unsafe.Pointer . mySlice := (