Home > Article > Backend Development > How to Output Unicode Strings Correctly in Windows Consoles with Go?
Outputting Unicode Strings Correctly in Windows Consoles with Go
When working with Go executables that print UTF-8 strings containing special characters in Windows console windows, you may encounter mangled output due to Windows' default IBM850 encoding. This encoding mismatch can result in unreadable characters that appear scrambled.
To ensure proper output, it is necessary to employ a method that handles Unicode strings correctly. One approach is to utilize the undocumented method WriteConsoleW provided by Windows' kernel32.dll.
The following Go code demonstrates how to print Unicode strings correctly in a Windows console:
<code class="go">import ( "sync/atomic" "time" ) func main() { handler := func(text []byte) { var prev []byte for { select { case s := <-strCh: if eq(prev, s) { atomic.AddUint64(&skips, 1) continue } time.Sleep(delay) prev = s r.add(s) default: time.Sleep(pollingDelay) } } } for i := 0; i < threads; i++ { go handler(data) } time.Sleep(timeout + 2e9) close(strCh) time.Sleep(2e9) m.printTotal(&report) }</code>
In this script:
The final statistics are printed using the printTotal method.
The above is the detailed content of How to Output Unicode Strings Correctly in Windows Consoles with Go?. For more information, please follow other related articles on the PHP Chinese website!