Undefined Title

Go言語 環境変数 展開

2013-09-01

環境変数の取得と、値の展開。

package main

import (
  "fmt"
  "os"
)

func replace(s string) string {
  switch s {
  case "name": return "Mike"
  default: return "hello"
  }
}

func main() {
  fmt.Println(os.Getenv("HOME"))
  fmt.Println(os.Expand("My name is ${name}.", replace))
  fmt.Println(os.ExpandEnv("My homedir is ${HOME}."))
}