Backspace角色在Go操场上不起作用

发布于 2025-01-21 16:53:01 字数 537 浏览 2 评论 0原文

我是新手。刚刚学习了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2025-01-28 16:53:01

您的程序准确输出了您告诉它的内容。问题主要是您的输出查看器。

控制字符和序列仅在发送到兼容的虚拟控制台(或物理终端,打印机或电视转播器;但后者如今非常罕见)时才具有预期效果。 Go Playground的作用是捕获程序的输出,并将其未修改发送到浏览器以显示。浏览器不会解释终端控制代码(Newline字符除外,甚至有时甚至是);相反,它预计将通过HTML标记传达格式。由于背面字符没有分配的字形,因此浏览器通常会显示占位符的字形,或者有时根本不会显示任何东西。

如果在本地计算机上运行GO程序时,您将获得类似的效果,然后将其输出重定向到文本文件中,然后在文本编辑器中打开文件:编辑器不会解释文本文件中包含的任何逃生序列;有时,它甚至会通过显示编辑器的终端来解释控制字符(如果恰好是基于控制台的编辑器),通过替换字符的符号,常规表示,例如^h

在中间示例中,'\ b'文字评估具有字符的Unicode代码点号的值(符合“符文”)的整数。这是在规范中解释

符文字面代表符文常数,一个识别Unicode代码点的整数值。符文的字面形式表示为一个或多个字符,如'x''\ n'中的单个引号中包含。在引号中,除了新线和未阐明的单报价外,任何字符都可能出现。单个引用的字符代表字符本身的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:

A rune literal represents a rune constant, an integer value identifying a Unicode code point. A rune literal is expressed as one or more characters enclosed in single quotes, as in 'x' or '\n'. Within the quotes, any character may appear except newline and unescaped single quote. A single quoted character represents the Unicode value of the character itself, while multi-character sequences beginning with a backslash encode values in various formats.

Since '\b' represents U+0008, what is passed to fmt.Println is the integer value 8. The function then prints the integer as its decimal representation, instead of interpreting it as a character code.

凉栀 2025-01-28 16:53:01

要查看的第一件事是您的终端“ \ b”是终端依赖性的,请检查运行程序的终端是否将其处理为“移动光标一个字符”(大多数Unixes like Will,我不知道Windows),您的第一个和第三个给定的示例准确地奏效了您在我的终端(ST)上的期望。

输入:fmt.println(“ hi”,'\ b',“在那里!”)

输出:HI 8!

预期:迄今为止!...假设符文不附加空格

! 做:

对于每个printf的功能,还有一个不采用格式的打印功能,相当于为每个操作数说%v。另一个变体println插入了操作数之间的空白,并附加了一个newline。

fmt handles %v作为%d,而不是%c,因此,'\ b'格式为“ 8”(ASCII值56 ),不是“ \ b”(ASCII值8)。如果符文在两个参数之间也将有一个空间。

对此输入的println做什么是:

打印字符串“ hi”

打印空间

格式编号8,然后打印字符串“ 8”

打印空间

打印字符串“ were!”

要调试诸如呈现不可见字符之类的问题,我建议您使用编码/hex package,例如:

package main

import (
    "encoding/hex"
    "fmt"
    "os"
)

func main() {
    d := hex.Dumper(os.Stdout)
    defer d.Close()

    fmt.Fprintln(d, "hi", '\b', "there!")
}

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).

input: fmt.Println("hi", '\b', "there!")

output: hi 8 there!

expected: hithere!... assuming runes are not appended with spaces

Here your assumption is not what package fmt does:

For each Printf-like function, there is also a Print function that takes no format and is equivalent to saying %v for every operand. Another variant Println inserts blanks between operands and appends a newline.

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:

package main

import (
    "encoding/hex"
    "fmt"
    "os"
)

func main() {
    d := hex.Dumper(os.Stdout)
    defer d.Close()

    fmt.Fprintln(d, "hi", '\b', "there!")
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文