我必须用golang解码一个字符串,其中包含西班牙语单词,带有重音和特殊字符。但是我尝试过的东西不起作用。你能不能引导我走上正确的道路,得到我需要的东西。提前谢谢。这是我当前的代码:
import (
"encoding/base64"
"fmt"
"golang.org/x/text/encoding/unicode"
)
var authStr = "2m5pY2E6U+06e1v28V19Okludml0YWNp824="
arB, _ := base64.StdEncoding.DecodeString(authStr)
fmt.Println("De arB se obtuvo: ")
fmt.Println(string(arB))
// got-> �nica:S�:{[��]}:Invitaci�n
// expected-> Única:Sí:{[öñ]}:Invitación
dec := unicode.UTF8.NewDecoder()
arButf8 := make([]byte, len(arB)*2)
if n, _, err := dec.Transform(arButf8, []byte(arB), true); err != nil {
fmt.Println("Error: ", err)
} else {
arButf8 = arButf8[:n]
fmt.Println("De authStr se obtuvo: ")
fmt.Println(string(arButf8))
// got-> �nica:S�:{[��]}:Invitaci�n
// expected-> Única:Sí:{[öñ]}:Invitación
}
// If do with Javascript atob("2m5pY2E6U+06e1v28V19Okludml0YWNp824=") works fine
这是正确的代码:
import (
"encoding/base64"
"fmt"
"golang.org/x/text/encoding/charmap"
)
var authStr = "2m5pY2E6U+06e1v28V19Okludml0YWNp824="
arB, _ := base64.StdEncoding.DecodeString(authStr)
fmt.Println("De arB se obtuvo: ")
fmt.Println(string(arB))
dec := charmap.ISO8859_1.NewDecoder()
arBdest := make([]byte, len(arB)*2)
if n, _, err := dec.Transform(arBdest, []byte(arB), true); err != nil {
fmt.Println("Error: ", err)
} else {
arBdest = arBdest[:n]
fmt.Println("De authStr se obtuvo: ")
fmt.Println(string(arBdest))
}