Is there a function built-in or in a supported package to get a *golang.org/x/text/encoding.Encoder
(Decoder
) based on an input charset string such as ISO-8859-1
or ISO-8859-15
? Right now the only way I see to do it is to match it myself:
func getEncoderForCharset(charset string) *encoding.Encoder {
switch charset {
case "ISO-8859-1":
return charmap.ISO8859_1.NewEncoder()
case "ISO-8859-15":
return charmap.ISO8859_15.NewEncoder()
// etc.
}
panic("Unknown charset \"" + charset + "\".")
}
See charset.Lookup(...)
, which returns a encoding.Encoding
, from which you can call NewEncoder()
and NewDecoder()
, e.g.:
ss := []string{"ISO-8859-1", "ISO-8859-15", "bogus"}
for _, s := range ss {
encoding, name := charset.Lookup(s)
if encoding != nil {
fmt.Printf("OK: encoding=%v, name=%q
", encoding, name)
// encoder := encoding.NewEncoder()
// decoder := encoding.NewDecoder()
}
}
// OK: encoding=&{Windows 1252}, name="windows-1252"
// OK: encoding=&{ISO 8859-15}, name="iso-8859-15"