These are the known aliases for the macintosh
encoding in Go:
var nameMap = map[string]htmlEncoding{
// ...
"csmacintosh": macintosh,
"mac": macintosh,
"macintosh": macintosh,
"x-mac-roman": macintosh,
// ...
}
Since macroman
is not in that list, you can use the CharsetReader
function field to use your custom list of aliases by setting
decoder.CharsetReader = charsetReader
where charsetReader
is:
func charsetReader(charset string, input io.Reader) (io.Reader, error) {
if isCharsetMacintosh(charset) {
return transform.NewReader(input, charmap.Macintosh.NewDecoder()), nil
}
return input, nil
}
var macNames = []string{
"macroman",
"csmacintosh",
"mac",
"macintosh",
"x-mac-roman",
}
func isCharsetMacintosh(charset string) bool {
charset = strings.ToLower(charset)
for _, n := range macNames {
if charset == strings.ToLower(n) {
return true
}
}
return false
}
If you need more information, the answers here might be helpful: Unmarshal an ISO-8859-1 XML input in Go. Also helpful was looking at the source code of the charset.NewReaderLabel
function and following the function calls.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…