我正在尝试使用C函数strfmon使用cgo。
工作的示例C代码是:
#include <stdio.h>
#include <monetary.h>
int main(void)
{
char str[100];
double money = 1234.56;
strfmon(str, 100, "%i", money);
printf("%s\n", string);
}到目前为止,我编写的Go代码是:
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include <monetary.h>
import "C"
import (
"fmt"
)
func main() {
str := [100]C.char{}
var money C.double = 1234.56
C.strfmon(str, 100, "%i", money)
fmt.Printf("%+v\n", str)
}当我go run main.go时,我得到以下错误:
./main.go:14:2: unexpected type: ...
我相信...指的是strfmon中的各种论点,但我不知道如何从Go开始解决这个问题。
发布于 2018-08-31 15:40:45
不支持调用变量C函数。可以通过使用C函数包装器来规避这一问题。
strfmon(3p)确实是一个可变的函数,签名中的...字符表示了这一点:
ssize_t strfmon(char *restrict s, size_t maxsize,
const char *restrict format, ...);因此,您可以在C中创建一个包装器函数,它有固定数量的参数,并根据需要调用strfmon(...),例如:
package main
// #cgo CFLAGS: -g -Wall
//
// #include <locale.h>
// #include <monetary.h>
// #include <stdlib.h>
//
// size_t format_amount(char * s, size_t maxsize, char * format, double amount)
// {
// setlocale(LC_ALL, "en_US");
// return strfmon(s, maxsize, format, amount);
// }
//
import "C"
import "fmt"
import "unsafe"
const SIZE = 100
func main() {
str := C.CString(string(make([]byte, SIZE)))
money := C.double(1234.56)
format := C.CString("[%n]")
C.format_amount(str, SIZE-1, format, money) // Call our wrapper here.
fmt.Printf("OK: %s\n", C.GoString(str))
// OK: [$1,234.56]
C.free(unsafe.Pointer(str))
C.free(unsafe.Pointer(format))
}https://stackoverflow.com/questions/52113027
复制相似问题