Backspace角色在Go操场上不起作用
我是新手。刚刚学习了fmt.println()
的各种用途。我尝试了以下内容在官方游乐场中,但产生了非常意外的输出。请解释一下我的理解中出错的地方。
输入:fmt.println(“ hi \ b”,“那里!”)
输出:hi。
H!
预期: 输出:HI 8!
预期:迄今为止!
...假设符文不附加空格
输入:fmt.println(“ hi”,“ \ bthere!”)
输出:嗨!
预期:迄今为止!
(注意:上图,占位符的角色已被U+FFFD替换,因为原始字符在环境之间并不始终如一。)
I am new to Go. Just learnt the various uses of fmt.Println()
. I tried the following stuff in the official playground but got a pretty unexpected output. Please explain where I have gone wrong in my understanding.
input: fmt.Println("hi\b", "there!")
output: hi� there!
expected: h there!
input: fmt.Println("hi", '\b', "there!")
output: hi 8 there!
expected: hithere!
... assuming runes are not appended with spaces
input: fmt.Println("hi", "\bthere!")
output: hi �there!
expected: hithere!
(Note: above, the placeholder character has been substituted by U+FFFD, as the original character does not render consistently between environments.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的程序准确输出了您告诉它的内容。问题主要是您的输出查看器。
控制字符和序列仅在发送到兼容的虚拟控制台(或物理终端,打印机或电视转播器;但后者如今非常罕见)时才具有预期效果。 Go Playground的作用是捕获程序的输出,并将其未修改发送到浏览器以显示。浏览器不会解释终端控制代码(Newline字符除外,甚至有时甚至是);相反,它预计将通过HTML标记传达格式。由于背面字符没有分配的字形,因此浏览器通常会显示占位符的字形,或者有时根本不会显示任何东西。
如果在本地计算机上运行GO程序时,您将获得类似的效果,然后将其输出重定向到文本文件中,然后在文本编辑器中打开文件:编辑器不会解释文本文件中包含的任何逃生序列;有时,它甚至会通过显示编辑器的终端来解释控制字符(如果恰好是基于控制台的编辑器),通过替换字符的符号,常规表示,例如
^h
。在中间示例中,
'\ b'
文字评估具有字符的Unicode代码点号的值(符合“符文”)的整数。这是在规范中解释:由于
'\ b'
表示U+0008,因此传递给fmt.println
的内容是整数值8。然后,功能将整数打印为其小数表示,而不是将其打印将其解释为字符代码。Your program outputs exactly what you told it to. The problem is mostly with your output viewer.
Control characters and sequences only have their expected effect when sent to a compatible virtual console (or a physical terminal, or a printer or teletypewriter; but the latter are pretty rare these days). What the Go playground does is capture the output of your program as-is and send it unmodified to the browser to display. The browser does not interpret terminal control codes (other than the newline character, and even that only sometimes); instead, it expects formatting to be conveyed via HTML markup. Since the backspace character does not have an assigned glyph, browsers will usually display a placeholder glyph instead, or sometimes nothing at all.
You would get a similar effect if, when running your Go program on your local machine, you redirected its output into a text file and then opened the file in a text editor: the editor will not interpret any escape sequence contained in the text file; sometimes it will even actively prevent control characters from being interpreted by the terminal displaying the editor (if it happens to be console-based editor), by substituting a symbolic, conventional representation of the character like
^H
.In the middle example, the
'\b'
literal evaluates to an integer with the value of the character’s Unicode code point number (what Go terms a ‘rune’). This is explained in the specification:Since
'\b'
represents U+0008, what is passed tofmt.Println
is the integer value 8. The function then prints the integer as its decimal representation, instead of interpreting it as a character code.要查看的第一件事是您的终端“ \ b”是终端依赖性的,请检查运行程序的终端是否将其处理为“移动光标一个字符”(大多数Unixes like Will,我不知道Windows),您的第一个和第三个给定的示例准确地奏效了您在我的终端(ST)上的期望。
! 做:
fmt handles
%v
作为%d
,而不是%c
,因此,'\ b'格式为“ 8”(ASCII值56 ),不是“ \ b”(ASCII值8)。如果符文在两个参数之间也将有一个空间。对此输入的println做什么是:
打印字符串“ hi”
打印空间
格式编号8,然后打印字符串“ 8”
打印空间
打印字符串“ were!”
要调试诸如呈现不可见字符之类的问题,我建议您使用
编码/hex
package,例如:output:output:
00000000 68 69 20 38 20 74 68 65 72 65 21 0a | hi 8在那里!|
游乐场: https://go.dev/play/p/f/f-if-i2mdh43k7
First thing to check out is your terminal, '\b' is terminal dependent, check if the terminal running your program handles that as "move cursor one character back" (most unixes-like will, i don't know about Windows), your first and third given example works exactly how your expectation is on my terminal (st).
Here your assumption is not what package fmt does:
Fmt handles
%v
for rune as%d
, not%c
, so '\b' is formatted as "8" (ascii value 56), not the '\b' (ascii value 8). Also runes will have a space if they are between two arguments.What Println does for this input is:
print string "hi"
print space
format number 8 then print string "8"
print space
print string "there!"
To debug problems like rendering invisible characters, I suggest you to use
encoding/hex
package, For example:Output:
00000000 68 69 20 38 20 74 68 65 72 65 21 0a |hi 8 there!.|
Playground: https://go.dev/play/p/F-I2mdh43K7