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 
mentioned a few lines later: "However, when printing a byte slice with a 
string-like verb (%s %q %x %X), it is treated ... as a single item."  So 
the "%02" applies to the whole byte slice, and not to each byte as I had 
wrongly thought.

All the best,
Jochen


On Saturday, 19 December 2020 at 00:24:07 UTC kortschak wrote:

> 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 bit string
> with mandated two leading zeros is 00.
>
> If you leave out the 02, you get the expected empty string.
>
> https://play.golang.org/p/uVFt3lecKxf
>
> On Fri, 2020-12-18 at 15:38 -0800, Marcin Romaszewicz wrote:
> > It's expected behavior.
> > 
> > Your for loop runs once for l=0, since your condition is <=0 because
> > len([]byte{}) is 0.
> > 
> > -- Marcin
>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/bd781856-6e04-4978-b23f-8b7d98611e56n%40googlegroups.com.

Reply via email to