I don't see that it matters much either way. It's clean enough as it is.
It's generated code, after all, and therefore is allowed to be ugly. Its
merit is not in the prettiness of the code it generates but in other
aspects, such as efficiency of both machine and programmer.

There may be one small advantage to the current plan: depending on the
quality of the compiler, making the variables global means they are
guaranteed to be initialized only once, rather than possibly once per call.
This could matter substantially for the index slice, which could be very
large.

Of course, the compiler could obviate that but is not required to.

-rob


On Sun, May 6, 2018 at 1:18 PM, <n...@mit.edu> wrote:

> I just learned about the stringer utility from Rob Pike's atricle about
> generate (https://blog.golang.org/generate).
>
> I was wondering why the code generated by stringer has it's data in global
> variables (example from the article)
>
> const _Pill_name = "PlaceboAspirinIbuprofenParacetamol"
>
> var _Pill_index = [...]uint8{0, 7, 14, 23, 34}
>
> instead of them being internal to the definition of
>
> func (i Pill) String() string
>
> The godoc for stringer (https://godoc.org/golang.org/x/tools/cmd/stringer)
> doesn't indicate that there are any other functions that these variables
> would be needed for and I think it would be cleaner to get them out of the
> global environment end encapsulate them to just where they're needed.
>
> Am I missing something?
>
> --
> 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.
>

-- 
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.

Reply via email to