Created
November 12, 2018 12:11
-
-
Save bamchoh/5f79c6a64d72505673310d21e387c535 to your computer and use it in GitHub Desktop.
Windowsの環境変数の一覧を取得
This file contains 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/exec" | |
"strings" | |
) | |
func splitKeyValue(environment string) (key, value string) { | |
e := strings.Split(environment, "=") | |
return e[0], e[1] | |
} | |
func getEnvList() map[string]string { | |
envList := make(map[string]string, 0) | |
output, err := exec.Command("cmd", "/c", "set").Output() | |
if err != nil { | |
fmt.Println(err) | |
} | |
environments := strings.Split(string(output), "\n") | |
for _, environment := range environments { | |
if environment == "" { | |
continue | |
} | |
k, v := splitKeyValue(environment) | |
envList[k] = v | |
} | |
return envList | |
} | |
func main() { | |
for k, v := range getEnvList() { | |
fmt.Println(k, "---", v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment