

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 10
click for more info
Not enough gems
Cost: 6 gems
1: Constants
incomplete
2: Computed Constants
incomplete
3: Comparing Go's Speed
incomplete
4: Formatting Strings in Go
incomplete
5: Runes and String Encoding
incomplete
6: Fix Bugs
incomplete
7: Fix Types
incomplete
8: Type Inference
incomplete
9: Format Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Go follows the printf tradition from the C language. In my opinion, string formatting/interpolation in Go is less elegant than Python's f-strings, unfortunately.
fmt.Printf() - Prints a formatted string to standard out.
fmt.Sprintf() - Returns the formatted string
These following "formatting verbs" work with the formatting functions above:
The %v variant prints any value in a default format. It can be used as a catchall.
s := fmt.Sprintf("I am %v years old", 10)
// I am 10 years old
s := fmt.Sprintf("I am %v years old", "way too many")
// I am way too many years oldIf you want to print in a more specific way, you can use the following formatting verbs:
s := fmt.Sprintf("I am %s years old", "way too many")
// I am way too many years olds := fmt.Sprintf("I am %d years old", 10)
// I am 10 years olds := fmt.Sprintf("I am %f years old", 10.523)
// I am 10.523000 years old
// The ".2" rounds the number to 2 decimal places
s := fmt.Sprintf("I am %.2f years old", 10.523)
// I am 10.52 years oldIf you're interested in all the formatting options, you can look at the fmt package's docs.
Create a new variable called msg on line 11 and use the appropriate formatting function to return a string that contains the following:
Hi NAME, your open rate is OPENRATE percentNEWLINEReplace NAME with the variable name,
Replace OPENRATE with the variable openRate rounded to the nearest "tenths" place, e.g 10.523 should be rounded down to 10.5
The word percent should appear as part of the string following the open rate value
Replace NEWLINE with the newline \n escape sequence.
For example, with the inputs "Jimmy McGill" and 2.5, the expected output would be:
Hi Jimmy McGill, your open rate is 2.5 percent