如何从Go语言调用这个C函数(使用Cgo工具)
这是这个 C 函数声明,
CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
我如何从 Go 调用这个函数?
type Easy struct {
curl unsafe.Pointer
code C.CURLcode
}
func (e *Easy)SetOption(option C.CURLoption, ...) {
e.code = C.curl_easy_setopt(e.curl, option, ????))
}
Here is this C function declaration
CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);
how do I call this function from Go?
type Easy struct {
curl unsafe.Pointer
code C.CURLcode
}
func (e *Easy)SetOption(option C.CURLoption, ...) {
e.code = C.curl_easy_setopt(e.curl, option, ????))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能直接调用它。 CGO 与 C 端的 vararg 函数不能很好地配合。
理想情况下,您可以创建一个 C 包装器,它接受您想要传递的选项列表。
然后,C 函数应将该列表扩展为
curl_easy_set_opt()
所需的变量参数。但我不确定这是否可能或如何去做。Go 函数的签名也不正确:
请注意,选项参数的类型已更改为其 go 版本。当有人使用您的包时,他们无权访问 C.xxx 类型。所以你不应该在公共 api 中使用它们。它们仅供您的包内部使用。
You can't call it directly. CGO does not play well with vararg functions on the C side.
Ideally, you could create a C wrapper which accepts a list of options you want to pass.
The C function should then expand that list into the variable arguments required by
curl_easy_set_opt()
. But I am not sure if that is possible or how to go about doing it.The signature for your Go function is also incorrect:
Note that the type of the option parameter has been changed to a go version of it. When someone uses your package, they have no access to the C.xxx types. So you should not use those in your public api. They are only meant for internal use in your package.