2021-02-23 02:40:43 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-06-06 05:05:05 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-02-23 02:40:43 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 05:05:05 +00:00
|
|
|
type StrFormatMap map[string]interface{}
|
|
|
|
|
2021-06-11 05:25:09 +00:00
|
|
|
// StrFormat formats the provided format string, replacing placeholders
|
|
|
|
// in the form of "{fieldName}" with the values in the provided
|
|
|
|
// StrFormatMap.
|
|
|
|
//
|
|
|
|
// For example,
|
2022-09-06 05:12:59 +00:00
|
|
|
//
|
|
|
|
// StrFormat("{foo} bar {baz}", StrFormatMap{
|
|
|
|
// "foo": "bar",
|
|
|
|
// "baz": "abc",
|
|
|
|
// })
|
2021-06-11 05:25:09 +00:00
|
|
|
//
|
|
|
|
// would return: "bar bar abc"
|
2021-06-06 05:05:05 +00:00
|
|
|
func StrFormat(format string, m StrFormatMap) string {
|
|
|
|
args := make([]string, len(m)*2)
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
args[i] = fmt.Sprintf("{%s}", k)
|
|
|
|
args[i+1] = fmt.Sprint(v)
|
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.NewReplacer(args...).Replace(format)
|
|
|
|
}
|