因此golang设计用于正确处理unicode/utf-8.
但是,我似乎有问题,正确地在终端的标准输出中打印出utf-8字符.
最简单的程序: -
package main
import "fmt"
func main() {
fmt.Println("Hello, ??")
}
Run Code Online (Sandbox Code Playgroud)
执行时,显示编码字符.
$ go run hello.go
Hello, ‰?ñÁïå
Run Code Online (Sandbox Code Playgroud)
我的终端的语言环境设置正确:
$ 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"
Run Code Online (Sandbox Code Playgroud)
我正在使用vim set encoding=utf-8并将其set fileencodings=utf-8包含在我的.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
Run Code Online (Sandbox Code Playgroud)
什么给出了什么?为什么我hello.go上面的vim创建的脚本打印出乱码,但我创建的nano hello2.go(包含相同的代码行)却没有?
$ file hello2.go
hello2.go: C source, UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)
事实上,当我打开vim-created hello.go时nano,源代码现在显示为: -
package main
import "fmt"
func main() {
fmt.Println("Hello ‰?ñÁïå")
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我打开相同的vim创建hello.go的vim,源代码读取: -
package main
import "fmt"
func main() {
fmt.Println("Hello, ??")
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
这些.vimrc是导致我这个问题的违规行.
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
Run Code Online (Sandbox Code Playgroud)
具体来说,tenc=macroman是在终端级别搞砸了我的编码.
切换到tenc=utf-8一切都很好.
在这个我应该看到这个即将到来的问题上,我浪费了4个小时的生命!啊.
| 归档时间: |
|
| 查看次数: |
1451 次 |
| 最近记录: |