当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Expand用法及代码示例

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!

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Expand。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。