Question: How to use '\b' with fmt.Println()?
The expected output as shown in the edit section:
Actual output:
Note the missing boxes (representing an unknown character?)
Question: How to use '\b' with fmt.Println()?
The expected output as shown in the edit section:
Actual output:
Note the missing boxes (representing an unknown character?)
Character escape \b
technically outputs Unicode character U+0008, so it doesn't delete previous character, but it adds a new character to the string.
Some environments (e.g., a VT100 compatible Unix terminal) will interpret U+0008 by moving the text caret one step towards left and overwrite the following content at that position (note that if nothing follows, there's nothing to overwrite, so this is not the same as delete).
If you execute the code in some other context that interprets the U+0008 as a visible character, you'll end up with some kind of representation of character U+0008 in the position you added the character escape \b
. Common options are some kind of broken graphics, "�" (U+FFFD replacement character), "␈" (U+2408 symbol for backspace) or an empty string.
In my experience, U+0008 getting interpreted as "delete previous character" in practice is pretty rare outside a terminal context.
This is how the character renders in my Firefox ESR 91 on Debian:
The backspace character clearly visible in the edit box, but not in the preview. However, if I select the text in the preview, copy and paste it, the backspace character is present. It just isn’t visible on the page.
Given how inconsistently it renders in various circumstances, perhaps it is better to avoid inputting it directly anyway. In this case, I think it is best to replace it by ‘�’, with a note below; this is what I ended up doing to the original question. In other circumstances, ‘␈’ might be a better replacement.