1

I'm trying to use the C function strfmon using cgo.

The example C code that works is:

#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);
}

The Go code I've written so far is:

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)
}

When I go run main.go I get the following error:

./main.go:14:2: unexpected type: ...

I believe the ... refers to the variadic argument in strfmon but I'm not sure how to work around that from Go.

4
  • There is no more to the error message
    – alexbilbie
    Aug 31, 2018 at 10:40
  • The ... refers to the variadic parameter in the C function. Take a look at man strfmon
    – alexbilbie
    Aug 31, 2018 at 11:11
  • Why not use x/text/currency or another third party library? Aug 31, 2018 at 13:11
  • Because I can't find a package that correctly formats an amount of money for a locale. e.g. in France the Euro symbol comes after the amount, in Germany it prefixes the amount
    – alexbilbie
    Aug 31, 2018 at 13:18

1 Answer 1

2

According to the cgo command documentation:

Calling variadic C functions is not supported. It is possible to circumvent this by using a C function wrapper.

And strfmon(3p) is indeed a variadic function as indicated by the ... characters in the signature:

ssize_t strfmon(char *restrict s, size_t maxsize,
   const char *restrict format, ...);

As such, you can create a wrapper function in C which has a fixed number of arguments and calls strfmon(...) as needed, for example:

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))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.