GO语言"os"包中"Expand"函数的用法及代码示例。
用法:
func Expand(s string, mapping func(string) string) string
Expand 根据映射函数替换字符串中的 ${var} 或 $var。例如,os.ExpandEnv(s) 等价于 os.Expand(s, os.Getenv)。
例子:
package main
import (
"fmt"
"os"
)
func main() {
mapper := func(placeholderName string) string {
switch placeholderName {
case "DAY_PART":
return "morning"
case "NAME":
return "Gopher"
}
return ""
}
fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper))
}
输出:
Good morning, Gopher!
相关用法
- GO ExpandEnv用法及代码示例
- GO Expm1用法及代码示例
- GO Exp2用法及代码示例
- GO Exp用法及代码示例
- GO Ext用法及代码示例
- GO Encode用法及代码示例
- GO EncodeToString用法及代码示例
- GO EscapeString用法及代码示例
- GO Encoder用法及代码示例
- GO Encoding.Decode用法及代码示例
- GO Errorf用法及代码示例
- GO Encoding.EncodeToString用法及代码示例
- GO Equal用法及代码示例
- GO Encoding.DecodeString用法及代码示例
- GO ErrReader用法及代码示例
- GO Encoding.Encode用法及代码示例
- GO EqualFold用法及代码示例
- GO EncodeRune用法及代码示例
- GO PutUvarint用法及代码示例
- GO Scanner.Scan用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Expand。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。