I'd like to write a little command-line utility to decode/encode streams of text, like iconv
, but in Go. The user will provide the encoder and decoder names and the utility will check them against charmap.All
to make the user args are valid before trying to decode/encode the stream.
I can iterate charmap.All
and print the names like:
for _, cmap := range charmap.All {
fmt.Println(cmap)
}
I can compare my cmap
var to a known Charmap:
if cmap == charmap.ISO8859_1 {
fmt.Println(charmap.ISO8859_1.String())
}
But I don't know how to do the next step, which seems so tantalizingly close (and easy):
var encoder string
flag.StringVar(&encoder, "encoder", "ISO 8859-1", "name of encoder")
flag.Parse()
for _, cmap := range charmap.All {
if cmap.String() == encoder {
// encode based on user's input
}
}
Is that possible, given the encoding/charmap API?
Also, how is it that my cmap
var and charmap.ISO8859_1
are equivalent (in the cmap == charmap.ISO8859_1
example), but cmap
is really an Encoding interface, and I cannot convert:
charmap.Charmap(cmap).String()
→ cannot convert cmap (type encoding.Encoding) to type charmap.Charmap
I'm still pretty new to Go, and don't fully understand these differences and equivalencies in types and interfaces.