I am writing a golang command line program that accepts user input. This input string has to be converted to UTF-8 and sent to another server for processing. On Linux, the terminal encoding is almost always UTF-8 but this does not seem to be the case in Windows. I tried setting the codepage on windows to 65001 using
chcp 65001
and also ensured the terminal font is set to Lucida console. However, the bytes read by
fmt.Scanf()
is not in UTF-8 format. I want to be able to detect the character encoding and convert the strings to UTF-8. Similarly, I should be able to convert from UTF-8 to the local encoding before printing to the screen.
Python seems to have "locale" package which can get the default encoding, decode and encode strings to any specified encoding. Is there an equivalent of this for golang?
Most of the stackoverflow discussions pointed at using chcp 65001 to change the encoding on windows terminal to UTF-8. This doesn't seem to work for me.
func main() {
foo := ""
fmt.Printf("Enter: ")
if _, err := fmt.Scanln(&foo) ; err != nil {
fmt.Println("Error while scanning: ", err)
}
fmt.Printf("Scanned bytes: % x", foo)
fmt.Println()
}
On Linux:
// ASCII
$ go run test.go
Enter: hello
Scanned bytes: 68 65 6c 6c 6f
// Unicode
$ go run test.go
Enter: ©
Scanned bytes: c2 a9
// Unicode
$ go run test.go
Enter: ΆΏΑΓΔΘΞ
Scanned bytes: ce 86 ce 8f ce 91 ce 93 ce 94 ce 98 ce 9e ce a3 ce a8 ce a9 ce aa ce ad ce b1 ce b2 ce ba
On Windows:
PS C:\> chcp
Active code page: 437
PS C:\> go run .\test.go
Enter: hello
Scanned bytes: 68 65 6c 6c 6f
PS C:\> go run .\test.go
Enter: ΆΏΑΓΔΘΞ
Scanned bytes: 3f 3f 61
// Change to Unicode
PS C:\> chcp 65001
Active code page: 65001
PS C:\> go run .\test.go
Enter: ΆΏΑΓΔΘΞ
Error while scanning: EOF
Scanned bytes:
Appreciate any help/pointers.
os.Getenv()
. – Peripherychcp 65001
to change the code page to UTF-8. Do you think this helps? – Peripheryfmt.Fscanf
with iconv'sReader
? You'd need to know before hand what the encoding is (maybe with environment variables). – Peripherynl_langinfo(CODESET)
with some C code in the go program. – Scrimp