Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
tip
What did you do?
$ go test
// file: foo_test.go
package foo
import (
"testing"
"golang.org/x/text/number"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
func TestPrint(t *testing.T) {
p := message.NewPrinter(language.English)
p.Printf("num %f", number.Decimal(1.0))
}
What did you expect to see?
Print "num 1"
What did you see instead?
./foo_test.go:13: Printf format %f has arg number.Decimal(1.0) of wrong type golang.org/x/text/number.Formatter
Package message is mostly plug-in compatible with fmt
. However, it has its own implementation of the parser which is more relaxed. In this case, the passed formatter implements an extended version of fmt.Formatter
, which is not compatible. It is possible fmt.Format
and cast it's State argument to the x/text version, but the more general point is that one cannot generally assume that format string passed to fmt
are the same as those passed to other packages.
For go test, the format vet checks should, regrettably, only apply to packages defined in core.
Other examples where x/text may differ:
- %s to accept numbers/dates to render them in written form.
- escape flags to print non-localized versions
- allows extra arguments (may be needed to pass gender value, for example). Could be circumvented using explicit position arguments.
See Issue #18085.