3 ms·
There's an odd, big complaint about numeric casts that culminates in this line of code: fmt.Fprintf(os.Stderr, "%"+strconv.FormatInt(int64(paddingWidth), 1
by pbnjay 11y ago
There's an odd, big complaint about numeric casts that culminates in this line of code:
fmt.Fprintf(os.Stderr, "%"+strconv.FormatInt(int64(paddingWidth), 10)+"s %s\r", "", legend)
Which could be improved greatly, and made a lot more readable with:
fmt.Fprint(os.Stderr, strings.Repeat(" ", paddingWidth), legend+"\r")
In my experience with Go over the last few years, any time I have this "ugh" moments it pays to take a step back. It's usually a code smell that there's a better way.
- cespare 11y agoYou can do better -- fmt provides a way to give a dynamic width or precision as an int argument. fmt.Fprintf(os.Stderr, "%*s %s\r", paddingWidth, "", legend) I'd further wonder whether a separate padding calculation is needed at all (too lazy to go read the source on Github at the moment).
- pbnjay 11y agoI started with that, but there's no reason to use the formatting specifiers when the padding string is already blank. With the dynamic width Fprintf still needs to waste time parsing the format string.
- markcrossfield 11y agoThanks pbnjay… I agree that’s much more readable and have improved the code.