将 strfmon 与 cgo 一起使用
Using strfmon with cgo
我正在尝试使用 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 中解决这个问题。
Calling variadic C functions is not supported. It is possible to circumvent this by using a C function wrapper.
而 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: [,234.56]
C.free(unsafe.Pointer(str))
C.free(unsafe.Pointer(format))
}
我正在尝试使用 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 中解决这个问题。
Calling variadic C functions is not supported. It is possible to circumvent this by using a C function wrapper.
而 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: [,234.56]
C.free(unsafe.Pointer(str))
C.free(unsafe.Pointer(format))
}