22

Question: How to use '\b' with fmt.Println()?

The expected output as shown in the edit section:

expected output

Actual output:

Note the missing boxes (representing an unknown character?)

Actual output

13
  • 3
    On my side, I don't even see the box: i.sstatic.net/Jnll1.png
    – Sabito
    Commented Apr 16, 2022 at 11:20
  • 1
    @Sabito錆兎standswithUkraine, I originally thought this was a browser issue, but I don't think so anymore for had this been one, the box wouldn't show up even in "This is how the post will look like" section of the edit mode. Commented Apr 16, 2022 at 14:24
  • @justanotherguy Do you still see it here? stackoverflow.com/posts/71892768/edit
    – Red
    Commented Apr 16, 2022 at 19:57
  • 4
    Don't even get me started. The number of times I've written a well-formatted post and had to go back and fix line breaks inside of inline formatted code when the preview showed it fine is enormous.
    – codewario
    Commented Apr 16, 2022 at 21:33
  • @AnnZen, no I don't Commented Apr 17, 2022 at 10:21
  • Afaik, there's some server side validations and cleaning.
    – Braiam
    Commented Apr 17, 2022 at 12:20
  • 4
    This is a bug, but have you tried a code block? Those are sometimes formatted a bit differently than inline code. Also, while you should post text as text as a general rule, if you have a scenario where you're unable to get the text to display what you're asking about, that would be one time where posting text as an image (in your original question) may actually be appropriate. Another option may be to replace the character with whatever character you're actually seeing, e.g. the square unicode character (□), but do be sure to note that you're doing this in the question.
    – NotThatGuy
    Commented Apr 17, 2022 at 16:13
  • Does it look like ‘�’ by any chance?
    – dumbass
    Commented Apr 17, 2022 at 18:13
  • 1
    Because what it actually is is the actual backspace character you expected to be output, going by <stackoverflow.com/revisions/…>.
    – dumbass
    Commented Apr 17, 2022 at 18:20
  • 1
    Similar Meta SE post (which I happen to have authored): Certain escape characters can be copied while editing, but not after Commented Apr 17, 2022 at 19:07
  • @NotThatGuy, yes. I have tried code-block too Commented Apr 18, 2022 at 5:09
  • How should arbitrary binary data be handled? Use hex dumps? Base64 encode it? Are Unicode control characters, e.g. BS, special? There must be plenty of meta posts about this. Commented Apr 18, 2022 at 8:07
  • 1
    I suggest literally dumping it as it. That happens to be usually the most helpful (and also the most annoying) way. Commented Apr 18, 2022 at 8:30

2 Answers 2

2

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.

1
2

This is how the character renders in my Firefox ESR 91 on Debian:

Screenshot of Stack Overflow’s post editor and preview; in the editor, the backspace character is displayed as a [0 0 0 8] code point box.  In the preview below it, the character is invisible.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .