Go treats all source code and string literals as UTF-8 by default, so you rarely need to manually handle ISO-8859-1 unless reading external binary data. Use the encoding package to convert byte slices from other encodings into UTF-8 strings before processing them.
package main
import (
"fmt"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
"io"
"os"
)
func main() {
// Read ISO-8859-1 file
f, _ := os.Open("data.iso8859")
defer f.Close()
// Create a reader that transforms ISO-8859-1 to UTF-8
reader := transform.NewReader(f, charmap.ISO8859_1.NewDecoder())
content, _ := io.ReadAll(reader)
fmt.Println(string(content))
}
Install the required dependency first:
go get golang.org/x/text/encoding/charmap