Use fmt.Sprintf with format verbs like %s and %d to build custom strings from variables in Go.
Use fmt.Sprintf with format verbs to construct strings from variables. The function returns the formatted string without printing it.
package main
import (
"fmt"
)
func main() {
name := "Alice"
age := 30
msg := fmt.Sprintf("Hello, %s! You are %d years old.", name, age)
fmt.Println(msg)
}
fmt.Sprintf builds a custom text string by combining plain text with variable data like numbers or names. It works like filling out a form where you insert specific details into pre-written blanks. You use it when you need to create a message dynamically before saving it or sending it elsewhere.