We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Formatting Strings in Go

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.

These following "formatting verbs" work with the formatting functions above:

Default Representation

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 old

If you want to print in a more specific way, you can use the following formatting verbs:

String

s := fmt.Sprintf("I am %s years old", "way too many")
// I am way too many years old

Integer

s := fmt.Sprintf("I am %d years old", 10)
// I am 10 years old

Float

s := 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 old

If you're interested in all the formatting options, you can look at the fmt package's docs.

Assignment

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 percentNEWLINE
  • Replace 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