How to ignore extra fields for fmt.Sprintf

后端 未结 5 1081
天涯浪人
天涯浪人 2021-01-13 13:24

I have a Golang program that reads a string parameter from command line and passes it to the fmt.Sprintf function. Let\'s say tmp_str is the target string from command line.

5条回答
  •  长情又很酷
    2021-01-13 13:36

    I agree with Volker's answer, but you could check your input string:

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        tmp_str := "hello %s"
    
        res := tmp_str
        if strings.Count(tmp_str, "%s") == 1 {
            res = fmt.Sprintf(tmp_str, "world")
        }
        fmt.Println(res)
    }
    

提交回复
热议问题