如何从Go语言调用这个C函数(使用Cgo工具)

发布于 2024-11-28 01:42:41 字数 345 浏览 1 评论 0原文

这是这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-12-05 01:42:41

你不能直接调用它。 CGO 与 C 端的 vararg 函数不能很好地配合。
理想情况下,您可以创建一个 C 包装器,它接受您想要传递的选项列表。
然后,C 函数应将该列表扩展为 curl_easy_set_opt() 所需的变量参数。但我不确定这是否可能或如何去做。

Go 函数的签名也不正确:

type Option C.CURLoption

func (e *Easy) SetOption(options ...Option) {
    // 'options' is now accessible as a slice: []Option
    // Turn this slice into a list of C.CURLoption pointers and pass it to
    // your C wrapper.

    if len(options) == 0 {
        return // No use continuing.
    }

    // Here is one way to convert the option slice to a list
    // that C can work with.
    size := int(unsafe.Sizeof(options[0]))
    list := C.malloc(C.size_t(size * len(options)))
    defer C.free(unsafe.Pointer(list)) // Free this after use!

    for i := range options {
        ptr := unsafe.Pointer( uintptr(list) + uintptr(size * i) )
        *(*C.CURLoption)(ptr) = C.CURLoption(options[i])
    }

    C.my_setopt_wrapper(e.curl, list, C.int(len(options)))
}

请注意,选项参数的类型已更改为其 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:

type Option C.CURLoption

func (e *Easy) SetOption(options ...Option) {
    // 'options' is now accessible as a slice: []Option
    // Turn this slice into a list of C.CURLoption pointers and pass it to
    // your C wrapper.

    if len(options) == 0 {
        return // No use continuing.
    }

    // Here is one way to convert the option slice to a list
    // that C can work with.
    size := int(unsafe.Sizeof(options[0]))
    list := C.malloc(C.size_t(size * len(options)))
    defer C.free(unsafe.Pointer(list)) // Free this after use!

    for i := range options {
        ptr := unsafe.Pointer( uintptr(list) + uintptr(size * i) )
        *(*C.CURLoption)(ptr) = C.CURLoption(options[i])
    }

    C.my_setopt_wrapper(e.curl, list, C.int(len(options)))
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文