Yes, you can do this by cutting the arguments you pass to the variable Sprintf:
func TruncatingSprintf(str string, args ...interface{}) (string, error) {
n := strings.Count(str, "%s")
if n > len(args) {
return "", errors.New("Unexpected string:" + str)
}
return fmt.Sprintf(str, args[:n]...), nil
}
func main() {
tmp_str := "hello %s %s %s"
str, err := TruncatingSprintf(tmp_str, "world")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(str)
}
Demonstration 1
2 ( , % s, )
, , , %%s. , , , templates ( , , , t ).