英文:
golang: why does terminal stdout not print out utf-8 characters?
问题
所以golang
被设计成能够正确处理unicode/utf-8。
然而,我似乎在我的终端标准输出中无法正确打印出utf-8字符。
这里是最简单的程序:-
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
执行时,显示编码字符。
$ go run hello.go
Hello, 世界
我的终端的区域设置是正确的:
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
我使用带有set encoding=utf-8
和set fileencodings=utf-8
的vim,并将其包含在我的.vimrc
文件中。
这可能实际上是一个vim
的问题。我使用nano
从头开始编写了这个脚本,并将其命名为hello2.go
,打印输出实际上正确地打印出了Hello, 世界
。但是原始的hello.go
脚本,使用vim
创建,只给我提供了无意义的Hello, ‰∏ñÁïå
。
所以,为了确保我的由vim创建的hello.go
脚本是UTF-8 Unicode文本,我在其上运行了file
命令。像这样:-
$ file hello.go
hello.go: C source, UTF-8 Unicode text
那么是什么原因呢?为什么我的由vim创建的上述hello.go
脚本打印出了无意义的字符,但是我的由nano创建的hello2.go
(包含完全相同的代码行)却没有?
$ file hello2.go
hello2.go: C source, UTF-8 Unicode text
事实上,当我用nano
打开由vim创建的hello.go
时,源代码现在是这样的:-
package main
import "fmt"
func main() {
fmt.Println("Hello 世界")
}
但是如果我用vim
打开相同的由vim创建的hello.go
,源代码是这样的:-
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
为什么会这样呢?
英文:
So golang
is designed to handle unicode/utf-8 properly.
However, I seem to have problem getting utf-8 characters printed out in my terminal's standard output correctly.
The simplest program here:-
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
when executed, shows encoded characters.
$ go run hello.go
Hello, 世界
My terminal's locale is set correctly:
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
and I am using vim with set encoding=utf-8
and set fileencodings=utf-8
included in my .vimrc
file.
This might actually be a vim
problem. I used nano
to write this script from scratch and name it hello2.go
and the print out actually prints Hello, 世界
correctly. But the original hello.go
script, created with vim
, only gives me the gibberish Hello, 世界
.
So just double check that my vim-created hello.go
script is a UTF-8 Unicode text, I run the file
command on it. Like this:-
$ file hello.go
hello.go: C source, UTF-8 Unicode text
So what gives? Why does my vim created hello.go
script above print out gibberish but my nano created hello2.go
(which contains the same exact lines of code) does not?
$ file hello2.go
hello2.go: C source, UTF-8 Unicode text
In fact, when I open vim-created hello.go
with nano
, the source code now reads:-
package main
import "fmt"
func main() {
fmt.Println("Hello 世界")
}
But if I open the same vim-created hello.go
with vim
, the source code reads:-
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Why is this so?
答案1
得分: 5
这是在我的.vimrc
文件中引起问题的冒犯行。
if has("gui_running")
set guitablabel=%t%=%m "设置标签的标签
set nomacatsui anti enc=utf-8 tenc=macroman gfn=Monaco:h11
" 设置窗口大小
set lines=40
set columns=120
else
set enc=utf-8 tenc=macroman gfn=Monaco:h11
set fenc=utf-8
endif
具体来说,tenc=macroman
在终端级别上搞乱了我的编码。
将其更改为tenc=utf-8
,一切都好了。
在这个我本应该预见到的问题上浪费了我4个小时的生命!唉。
英文:
These are the offending lines in my .vimrc
that is causing me this problem.
if has("gui_running")
set guitablabel=%t%=%m "Set the label of the tabs
set nomacatsui anti enc=utf-8 tenc=macroman gfn=Monaco:h11
" set window size
set lines=40
set columns=120
else
set enc=utf-8 tenc=macroman gfn=Monaco:h11
set fenc=utf-8
endif
Specifically, tenc=macroman
is screwing up my encoding at terminal level.
Switched it to tenc=utf-8
and all is good.
Wasted 4 hours of my life on this I-should-have-seen-this-coming problem! Ugh.
答案2
得分: 1
最有可能的问题与Windows的“命令提示符”(控制台)工具属性中使用的字体有关。
点击左上角的菜单图标,选择“属性”,然后选择“字体”选项卡。在我的Windows 8.1机器上,默认字体是“光栅字体”,而我得到的是问号?????而不是由Go Lang程序中的UTF8编码的Unicode字符。
解决方案是选择其中一种TrueType字体,“Consolas”或“Lucida Console”。
英文:
Most likely the issue is related to Font used in Windows "Command Prompt" (Console) tool properties.
Click on menu icon in top left corner, select "Properties",
then "Font" tab. On my Windows 8.1 machine default was "Raster Font" and I was getting question marks ????? instead of Unicode characters, encoded by UTF8 in Go Lang program.
The solution is to select one of TrueType fonts, "Consolas" or "Lucida Console"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论