How to print UTF-8 (or unicode) characters in Go (golang) on Windows

Issue

Let’s have a look at this:

✓ Hello, 世界

As you can see there is a unicode checkmark and chinese/japanese characters. In go, If I use MSYS or linux environment i can easily print those characters. Even on windows. However, I am unable to see them in CMD nor in Powershell.

I got this:

powershell

This is my very basic code:

package main

import (
    "fmt"
)

func main() {

    fmt.Println("✓ Hello, 世界")
    // OR
    fmt.Println("\u2713 Hello, 世界")
}

Also, I have dozens of console apps and they are able to show such characters on my windows using cmd or powershell. Why go can’t?

Solution

The problem for the Windows cmd and PowerShell consoles is the lack of CJK characters in fonts such as Consolas and Lucida Console. On Windows 10, change the console font to a font that supports CJK characters, for example, NSimSun or SimSun-ExtB.

For example,

font.go:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("✓ Hello, 世界")
    // OR
    fmt.Println("\u2713 Hello, 世界")
}

Output (NSimSun):

Microsoft Windows [Version 10.0.17134.345]
>go version
go version devel +47cc59f31f Tue Oct 23 00:29:57 2018 +0000 windows/amd64
>go run font.go
✓ Hello, 世界
✓ Hello, 世界
>

Answered By – peterSO

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.