On Wed, Jun 7, 2017 at 11:12 AM, <hani...@gmail.com> wrote: > > I was perusing the code for the standard log package (src/log/log.go - The > Go Programming Language) and was wondering why the formatHeader method (line > 92) takes a pointer to byte slice since the only call to this method line > (163) calls it with the address of its own byte slice member (and it only > has one such member). > > To be clear, I'm not wondering why formatHeader takes a pointer to a byte > slice instead of a byte slice--indeed the itoa function above it does this, > and must do this--but rather why does it need the buffer parameter at all, > since it's a method and therefore already has access to its buffer. > > Is it simply to make the method more general, for possible future changes, > so that it can format headers using its flag and prefix settings but write > the header to some other buffer instead of its own? Or is there a deeper > design reason that I'm missing? > > Please note that I'm not trying to criticize but rather trying to > understand. I often look at how code is written in the standard library to > improve my own code.
I think it's just a historical accident. Originally Logger.Output passed the address of a local variable to formatHeader. Then in https://golang.org/cl/4363045 Rob added a buf field to Logger, and passed the address of that, with the CL description saying "rearrange the code a little so we can avoid allocating a buffer on every call." He could have changed formatHeader then to stop passing the buffer, but he either didn't notice or decided not to mix that into the same change. 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. For more options, visit https://groups.google.com/d/optout.