String formatting has precision in Go?

I came across this line in the book 'The Go Programming Languague' on page 112.fmt.Printf("#%-5d %9.9s %.55s\n", item.Number, item.User.Login, item.Title)What do %9.9s and %.55s mean?

1 Answer

From go doc fmt:

Width is specified by an optional decimal number immediately preceding the verb. If absent, the width is whatever is necessary to represent the value. .... For strings, byte slices and byte arrays, however, precision limits the length of the input to be formatted (not the size of the output), truncating if necessary.

Thus, %.9.9s means minimal width 9 runes with input truncated at 9, and thus exactly length 9. Similar %.55s means no minimal width but input truncated at 55 which means output is at most 55 runes.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like