Sprintf and Fprintf
Format to strings and writers.
The Printf Family
The fmt package has three formatting cousins:
Printfwrites to standard output.Sprintfreturns a formatted string.Fprintfwrites to any writer.
package main
import "fmt"
func main() {
fmt.Printf("hello %s\n", "world")
}Sprintf Returns a String
fmt.Sprintf formats exactly like Printf but, instead of printing, it returns the result as a string you can store or reuse.
package main
import "fmt"
func main() {
msg := fmt.Sprintf("%s scored %d", "Bob", 95)
fmt.Println(msg)
}All lessons in this course
- Printf Verbs
- Sprintf and Fprintf
- The Stringer Interface
- Scanning Input