Formatting strings
The Go standard library offers multiple ways to substitute values in a text template. Here, we will discuss the text formatting utilities in the fmt package. They offer a simple and convenient way to substitute values in a text template.
How to do it...
- Use the
fmt.Printfamily of functions to format values fmt.Printwill print a value using its default formatting- A string value will be printed as is
- A numeric value will be first converted to a string as an integer, a decimal number, or by using scientific notation for large exponents
- A Boolean value will be printed as
trueorfalse - Structured values will be printed as a list of fields
If a Print function ends with ln (such as fmt.Println), a new line will be output after the string.
If a Print function ends with f, the function will accept a format argument, which will be used as the template into which it will substitute values.
fmt.Sprintf will format a string and return...