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.goEnter: helloScanned bytes: 68 65 6c 6c 6f// Unicode$ go run test.goEnter: ©Scanned bytes: c2 a9// Unicode$ go run test.goEnter: ΆΏΑΓΔΘΞ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:\> chcpActive code page: 437PS C:\> go run .\test.goEnter: helloScanned bytes: 68 65 6c 6c 6fPS C:\> go run .\test.goEnter: ΆΏΑΓΔΘΞScanned bytes: 3f 3f 61// Change to UnicodePS C:\> chcp 65001Active code page: 65001PS C:\> go run .\test.goEnter: ΆΏΑΓΔΘΞError while scanning: EOFScanned bytes:
Appreciate any help/pointers.