티스토리 뷰

공부

[golang] env -> variable

승가비 2022. 8. 24. 11:30
728x90
package main

import (
   "os"
   "strings"
   "text/template"
)

func Format(s string, v interface{}) string {
   t, b := new(template.Template), new(strings.Builder)
   template.Must(t.Parse(s)).Execute(b, v)

   result := b.String()
   if result == "<no value>" {
      return s
   }

   return result
}

func KeyValue(item string) (key, val string) {
   splits := strings.Split(item, "=")
   key = splits[0]
   val = splits[1]
   return
}

func Env() map[string]string {
   data := os.Environ()

   items := make(map[string]string)
   for _, item := range data {
      key, val := KeyValue(item)
      items[key] = val
   }

   return items
}

func main() {
   response := Format("{{ .asdf }}", Env())

   println(response)
}

https://stackoverflow.com/questions/19612449/default-value-in-gos-method

 

Default value in Go's method

Is there a way to specify default value in Go's function? I am trying to find this in the documentation but I can't find anything that specifies that this is even possible. func SaySomething(i str...

stackoverflow.com

https://stackoverflow.com/questions/19761393/why-does-go-have-typed-nil

 

Why does Go have typed nil?

Why does Go have typed nil? It throws an explicit interface conformation check for convenience. What's the problem of untyped nil and what did the designers want to solve with typed nil?

stackoverflow.com

https://gist.github.com/naveensrinivasan/5846791

 

Here is a code to get the environment variables as a map in go instead of slice.

Here is a code to get the environment variables as a map in go instead of slice. - environmentasmap.go

gist.github.com

 

728x90
댓글