String formatting

When formatting strings in Go, you can use the fmt package, which provides functions like fmt.Sprintf for returning formatted strings and fmt.Printf for printing formatted strings to the console.

There are various verbs you can use to format strings, such as:

  • %s for strings
  • %d for integers
  • %f for floating-point numbers, e.g. .2f for two decimal places
  • %v for the default format of any value
name := "John"
age := 30
formattedString := fmt.Sprintf("My name is %s and I am %d years old.", name, age)
fmt.Println(formattedString) // Output: My name is John and I am 30 years old.