Skip to Content
Learn Go: fmt Package
The Sprintf Method

If we need to interpolate a string, without printing it, then we can use fmt.Sprintf().

Just like fmt.Printf(), fmt.Sprintf() can also use verbs:

correctAns := "A" answer := fmt.Sprintf("And the correct answer is… %v!", correctAns) fmt.Print(answer) // Prints: And the correct answer is… A!

fmt.Sprintf() works very similarly to fmt.Printf(), the major difference is that fmt.Sprintf() returns its value instead of printing it out!



Assign to wish the value of calling fmt.Sprintf() with the values template and pet.

wish should then contain the interpolated string "I wish I had a puppy.".

Folder Icon

Take this course for free

Already have an account?