Home >Backend Development >Golang >How to set Chinese in golang
How to display Chinese in Go? First, set the environment variable LC_ALL to zh_CN.UTF-8. Second, load and apply fonts that support Chinese (such as Microsoft Yahei). Other notes: Go 1.16 and above have enhanced support for UTF-8, and Windows systems need to set the console encoding to UTF-8.
How to display Chinese in Go
Question: What is needed to display Chinese in Go What settings should be made?
Answer: To display Chinese in Go, you need to set the following two aspects:
1. Set environment variables
First, you need to set the environment variable LC_ALL
to zh_CN.UTF-8
to specify the Chinese locale using UTF-8 encoding. You can use the following code to set environment variables at the beginning of the script:
<code class="go">import "os" func main() { os.Setenv("LC_ALL", "zh_CN.UTF-8") }</code>
2. Set the font
Secondly, you need to set the font to a font that supports Chinese. Fonts can be loaded using the font.NewFace
function and applied to windows or other graphical controls using the SetFace
function. The following example loads the msyh
font (Microsoft Yahei):
<code class="go">import ( "log" "golang.org/x/image/font" "golang.org/x/image/font/opentype" ) func main() { f, err := opentype.Parse("msyh.ttf") if err != nil { log.Fatal(err) } wf := font.NewFace(f, &font.Options{ Size: 12, }) }</code>
Other notes:
chcp 65001
in the command prompt to set it. The above is the detailed content of How to set Chinese in golang. For more information, please follow other related articles on the PHP Chinese website!