1
我試圖從Go中調用一些ioctl,其中一些字符串以C字符串作爲參數。例如,在C:將字符串轉換爲沒有CGo的C字符串
/* When the user asks to bind a message name to an interface, they use: */
struct kbus_bind_request {
__u32 is_replier; /* are we a replier? */
__u32 name_len;
char *name;
};
extern int kbus_ksock_bind(kbus_ksock_t ksock,
const char *name,
uint32_t is_replier)
{
int rv;
kbus_bind_request_t bind_request;
bind_request.name = (char *) name;
bind_request.name_len = strlen(name);
bind_request.is_replier = is_replier;
rv = ioctl(ksock, KBUS_IOC_BIND, &bind_request);
if (rv < 0)
return -errno;
else
return rv;
}
我轉換結構將轉至STRUC這樣的:
type kbus_bind_request struct {
is_replier uint32 /* are we a replier? */
name_len uint32
name unsafe.Pointer // char*
}
現在,我怎麼轉換轉到string
存儲在一個unsafe.Pointer
C字符串?我不想使用CGo,因爲我正在交叉編譯,這讓事情變得很痛苦。
人們通常會使得'Name'領域的'* byte'而不是'unsafe.Pointer'。在syscall包中查找示例,如下所示:https://golang.org/pkg/syscall/#Iovec – JimB
啊,這樣做更有意義! – Timmmm