-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
40 lines (34 loc) · 1019 Bytes
/
format.go
File metadata and controls
40 lines (34 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @Author: leafney
* @GitHub: https://github.com/leafney
* @Project: rose
* @Date: 2023-08-10 15:06
* @Description:
*/
package rose
import (
"fmt"
"strings"
)
type FmtItems map[string]interface{}
// FmtString Similar to formatting strings with named parameters in Python: `replace {msg} with {data}` {"msg":"hello","data":123}
func FmtString(template string, items FmtItems) string {
for key, value := range items {
template = strings.ReplaceAll(template, "{"+key+"}", fmt.Sprintf("%v", value))
}
return template
}
// FmtStringWith Similar to formatting strings with named parameters in Python;
// prefix default value `{`, suffix default value `}`
func FmtStringWith(prefix, suffix, template string, items FmtItems) string {
if len(prefix) == 0 {
prefix = "{"
}
if len(suffix) == 0 {
suffix = "}"
}
for key, value := range items {
template = strings.ReplaceAll(template, fmt.Sprintf("%s%s%s", prefix, key, suffix), fmt.Sprintf("%v", value))
}
return template
}