在Go中使用CGO调用第三方C库,出现如下C语言的struct
struct pcap_rmtauth
{
int type;
char *username;
char *password;
};
然后使用Cgo调用
var a *C.pcap_rmtauth
a.type // 此处会报错,因为type是Go中的关键字
请问如何解决?
在Go中使用CGO调用第三方C库,出现如下C语言的struct
struct pcap_rmtauth
{
int type;
char *username;
char *password;
};
然后使用Cgo调用
var a *C.pcap_rmtauth
a.type // 此处会报错,因为type是Go中的关键字
请问如何解决?
如果是自己写的,建议直接换个名字,不是个好习惯,如果是用的第三方库,可以参考:https://golang.org/src/cmd/cg...
Within the Go file, C's struct field names that are keywords in Go
can be accessed by prefixing them with an underscore: if x points at a C struct with a field named "type", x._type accesses the field.
C struct fields that cannot be expressed in Go, such as bit fields
or misaligned data, are omitted in the Go struct, replaced by
appropriate padding to reach the next field or the end of the struct.
7 回答5.3k 阅读
6 回答6.8k 阅读✓ 已解决
4 回答2.3k 阅读
1 回答3.3k 阅读
2 回答886 阅读✓ 已解决
2 回答2.2k 阅读
1 回答2.1k 阅读
已经找到答案: