Created
June 23, 2013 22:36
-
-
Save naveensrinivasan/5846791 to your computer and use it in GitHub Desktop.
Here is a code to get the environment variables as a map in go instead of slice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "strings" | |
| ) | |
| func main() { | |
| getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string { | |
| items := make(map[string]string) | |
| for _, item := range data { | |
| key, val := getkeyval(item) | |
| items[key] = val | |
| } | |
| return items | |
| } | |
| environment := getenvironment(os.Environ(), func(item string) (key, val string) { | |
| splits := strings.Split(item, "=") | |
| key = splits[0] | |
| val = splits[1] | |
| return | |
| }) | |
| fmt.Println(environment["KEY"]) | |
| } |
a=b=c want: a b=c
a=b=c want: a b=c
package main
import (
"fmt"
"os"
"strings"
)
func main() {
getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string {
items := make(map[string]string)
for _, item := range data {
key, val := getkeyval(item)
items[key] = val
}
return items
}
environment := getenvironment(os.Environ(), func(item string) (key, val string) {
splits := strings.Split(item, "=")
key = splits[0]
val = strings.Join(splits[1:],"=")
return
})
fmt.Println(environment["KEY"])
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome!
how it is?