在C++调用go 编写的lib时出现了一个问题,这边把一段[]byte转成string,然后通过CGO的C.CString函数转成char* 返回给C调用处。发现发挥的char*好像被截断了,打印不出来任何值,但是在go的一侧,不论是打印[]byte,还是打印string都没有问题。并不是每次都会出现这个问题,但是这次出现了。
```go
//go 那边的代码如下
val, err := txn.Get(context.TODO(), []byte(key))
if err != nil {
if use == 0{
defer CloseTxn(client, index)
}
none := C.CString("")
defer C.free(unsafe.Pointer(none))
if err == kv.ErrNotExist{
return ErrorKeyNotExist, none
}
return ErrorGetKeyFail, none
}
// todo: 0 close,1 continue use
if use == 0{
defer CloseTxn(client, index)
}
fmt.Println(val) // 这里可以打印出来
sval := string(val)
fmt.Println(sval) // 这里也可以打印出来string
res := C.CString(sval)
fmt.Println(res) // 这里会打印指针地址
defer C.free(unsafe.Pointer(res))
return OperatorSuccess, res
```
```cpp
// C++的调用方式为
std::string key = "f590ca8a-0283-4389-a769-5d66b8be72be.546733.1_wlf1";
char *tarKey = const_cast<char*>(key.c_str());
int newStrSize = key.size();
cout<<"start get"<<endl;
GetKey_return getRes = ((FUNC_GETKEY)funcMap["GetKey"])(1, beginRes, buildGoString(tarKey, newStrSize), 0);
std::cout<<"res is " << getRes.r1 << std::endl; //这里什么都打印不出来!!!!
struct GetKey_return {
GoInt32 r0;
char* r1;
};
```
打印出来的[]byte如下
[11 3 233 2 0 0 1 1 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 8 3 97 0 0 0 1 173 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 51 52 54 53 51 100 102 99 54 53 99 52 57 99 99 97 48 102 102 56 54 97 100 101 99 49 97 101 98 54 54 48 2 0 0 0 108 113 173 71 0 0 0 0 0 0 6 0 0 0 109 97 115 116 101 114 8 0 0 0 83 84 65 78 68 65 82 68 0 0 0 0 0 0 0 0 45 0 0 0 102 53 57 48 99 97 56 97 45 48 50 56 51 45 52 51 56 57 45 97 55 54 57 45 53 100 54 54 98 56 98 101 55 50 98 101 46 54 54 50 48 57 53 46 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 12 0 0 0 117 115 101 114 46 114 103 119 46 97 99 108 113 0 0 0 3 2 107 0 0 0 3 2 12 0 0 0 2 0 0 0 108 113 2 0 0 0 108 113 3 3 79 0 0 0 1 1 0 0 0 2 0 0 0 108 113 15 0 0 0 1 0 0 0 2 0 0 0 108 113 4 3 44 0 0 0 2 2 4 0 0 0 0 0 0 0 2 0 0 0 108 113 0 0 0 0 0 0 0 0 2 2 4 0 0 0 15 0 0 0 2 0 0 0 108 113 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 117 115 101 114 46 114 103 119 46 99 111 110 116 101 110 116 95 116 121 112 101 1 0 0 0 0 13 0 0 0 117 115 101 114 46 114 103 119 46 101 116 97 103 33 0 0 0 51 52 54 53 51 100 102 99 54 53 99 52 57 99 99 97 48 102 102 56 54 97 100 101 99 49 97 101 98 54 54 48 0 17 0 0 0 117 115 101 114 46 114 103 119 46 109 97 110 105 102 101 115 116 35 1 0 0 6 3 29 1 0 0 173 71 0 0 0 0 0 0 36 0 0 0 119 108 102 49 46 121 120 77 85 109 105 74 86 53 121 79 105 84 105 122 110 86 48 111 77 97 98 45 107 105 76 68 118 71 122 105 1 0 0 0 0 0 0 0 0 0 0 0 2 1 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0 0 0 0 0 0 0 0 0 45 0 0 0 102 53 57 48 99 97 56 97 45 48 50 56 51 45 52 51 56 57 45 97 55 54 57 45 53 100 54 54 98 56 98 101 55 50 98 101 46 53 52 54 55 51 51 46 49 1 1 132 0 0 0 36 0 0 0 53 100 52 56 55 102 52 56 45 57 100 98 99 45 49 49 101 98 45 57 98 48 56 45 49 52 49 56 55 55 53 50 57 100 97 100 36 0 0 0 49 99 53 50 50 102 99 48 45 57 100 98 99 45 49 49 101 98 45 98 49 53 98 45 49 52 49 56 55 55 53 50 57 100 97 100 0 0 0 0 36 0 0 0 53 100 52 56 55 102 52 56 45 57 100 98 99 45 49 49 101 98 45 57 98 48 56 45 49 52 49 56 55 55 53 50 57 100 97 100 0 0 0 0 0 0 0 0 0 1 1 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
将上面的[]byte转换成string后打印如下
aG 34653dfc65c49cca0ff86adec1aeb660lqGmasteSTANDARD-f590ca8a-0283-4389-a769-5d66b8be72be.662095.2 user.rgw.aclqk user.rgw.etag!34653dfc65c49cca0ff86adec1aeb660user.rgw.manifest#G$wlf1.yxMUmiJV5yOiTiznV0oMab-kiLDvGzi @-f590ca8a-0283-4389-a769-5d66b8be72be.546733.1$5d487f48-9dbc-11eb-9b08-141877529dad$1c522fc0-9dbc-11eb-b15b-141877529dad$5d487f48-9dbc-11eb-9b08-141877529dad
更多评论