golang cgo字符串转换 - Fri, Aug 11, 2023
golang cgo字符床转换
1. string to char*
ptChar := C.CString("hello world")
2. string to const char*
#include <stdio.h>
void Hello(const char* s) {
printf("%s\n", s);
}
C.Hello(C.CString("Hello, World\n"))
3. []string to char**
authArray := []*C.char{}
for _, v := range in.AuthArray {
cs := C.CString(v)
authArray = append(authArray, cs)
}
defer func() {
for _, v := range authArray {
C.free(unsafe.Pointer(v))
}
}()
ptAuthArray := &authArray[0]