On Thu, May 27, 2021 at 2:22 PM Terry Phelps <tgphelp...@gmail.com> wrote:
>
>
> My background. I've written a lot of C and Python (and other things), but am 
> new to Go.
>
> I am getting results from fmt.Fprintf that I can't understand. Here's what 
> I'm puzzled about:
>
> I wrote, in Go, a file hex-and-ASCII dumper of the kind everyone has seen. 
> (Like the Unix xxd command, for example.) I've been using it, and it DOES 
> work. The output looks like this:
>
> 00000000 2f 2f 20 50 61 63 6b 61 67 65 20 68 64 75 6d 70   // Package hdump
> 00000010 20 64 75 6d 70 73 20 64 61 74 61 20 69 6e 20 74    dumps data in t
> 00000020 68 65 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 68   he traditional h
>
> The dumping is done by a function that accepts a count and a []bytes, and an 
> io.Writer to send the output to. I pass os.Stdout to the function, and the 
> hex/ASCII you see above is printed. I won't show all that code, but perhaps 
> you can trust me that I've used it for weeks with no problems.
>
> Then I started reading about how good Go programmers write unit tests, and I 
> wrote a simple test. Of course, I need to capture the output, instead of 
> sending it to os.Stdout. I read about strings.Builder, and that seems to be 
> what I need. As a first attempt, I'll send the same parameters to my 
> function, one going to stdout and other to a strings.Builder. I'll print the 
> contents of the string.Builder and it should match what gets sent to stdout.
>
> Here is the code:
>
> func TestHdump(t *testing.T) {
>     var b strings.Builder
>
>     dest := NewHdumper(&b)
>     dest.DumpBytes(21, []byte("\t123456789abcdefHello"))
>     fmt.Printf(b.String())
>
>     fmt.Printf("for real:\n")
>     dest = NewHdumper(os.Stdout)
>     dest.DumpBytes(21, []byte("\t123456789abcdefHello"))
> }
>
> Quite simple: do it once to the strings.Builder and once to stdout, and 
> visually compare them. (I'll make it a real test, after I sort this out.) But 
> the output is VERY different, and I can't figure out why:
>                                  00000000 09 31 32 33 34 35 36 37 38 39 61 62 
> 63 64 65 66   .123456789abcdef
> 00000010 48 65 6c 6c 6f   Hello
> for real:
> 00000000 09 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66   .123456789abcdef
> 00000010 48 65 6c 6c 6f                                    Hello
>
> There are two big differences:
> 1. The first line of 16 bytes is indented (a lot) when going to the 
> strings.Builder.
> 2. The "Hello" chars are not "pushed out to the right" to align with the 
> ASCII characters of the line above.
>
> Now, why aren't these two things absolutely identical? HELP!

There are many possibilities.  In order to help you, we'll need to see
your code.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVE0OVA6GmkuRqqQs1pPktTbrMO2%2BCXKQMKcZtiASUDSQ%40mail.gmail.com.

Reply via email to