Dear all,
Thanks for your help. The solution is indeed to use "%x" instead of "%02x".
My confusion was caused by the following sentence in the docs: "For
compound operands such as slices and structs, the format applies to the
elements of each operand". I never spotted the relevant exception
I think that's the question. Here's a simpler example,
https://play.golang.org/p/9Kv3PhlM-OF
That is, is 00 an expected %02x representation of a zero-length byte
slice?
The answer to that is yes; the 02 forces leading zeros. The %x verb
essentially renders bit strings as hex, so a zero-length
It's expected behavior.
Your for loop runs once for l=0, since your condition is <=0 because
len([]byte{}) is 0.
-- Marcin
On Fri, Dec 18, 2020 at 3:28 PM Jochen Voss wrote:
> Hello,
>
> I can print slices of bytes as hex strings, using code like the following:
>
> x := []byte{0, 1, 2, 3}
>
Hello,
I can print slices of bytes as hex strings, using code like the following:
x := []byte{0, 1, 2, 3}
fmt.Printf("%02x", x[:l])
This gives the output "00010203" as expected. But this fails for the empty
slice: running
x := []byte{}
fmt.Printf("%02x", x[:l])
gives "00" instead of