1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 14:42:27 +08:00

feat: add TemplateReplace

This commit is contained in:
dudaodong
2024-09-06 11:04:54 +08:00
parent f9e5ec9096
commit 93be25920f
5 changed files with 151 additions and 1 deletions

View File

@@ -1658,4 +1658,38 @@ func main() {
// oHell
// loHel
}
```
### <span id="TemplateReplace">TemplateReplace</span>
<p>将模板字符串中的占位符替换为数据映射中的相应值。占位符括在花括号中,例如 {key}。例如模板字符串为“Hello, {name}!”,数据映射为{"name": "world"}结果将为“Hello, world!”。</p>
<b>函数签名:</b>
```go
func TemplateReplace(template string, data map[string]string) string
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
template := `Hello, my name is {name}, I'm {age} years old.`
data := map[string]string{
"name": "Bob",
"age": "20",
}
result := strutil.TemplateReplace(template, data)
fmt.Println(result)
// Output:
// Hello, my name is Bob, I'm 20 years old.
}
```