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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。