github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/go/env.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "os" 10 "runtime" 11 "strings" 12 ) 13 14 var cmdEnv = &Command{ 15 Run: runEnv, 16 UsageLine: "env [var ...]", 17 Short: "print Go environment information", 18 Long: ` 19 Env prints Go environment information. 20 21 By default env prints information as a shell script 22 (on Windows, a batch file). If one or more variable 23 names is given as arguments, env prints the value of 24 each named variable on its own line. 25 `, 26 } 27 28 type envVar struct { 29 name, value string 30 } 31 32 func mkEnv() []envVar { 33 var b builder 34 b.init() 35 36 env := []envVar{ 37 {"GOARCH", goarch}, 38 {"GOBIN", gobin}, 39 {"GOCHAR", archChar}, 40 {"GOEXE", exeSuffix}, 41 {"GOHOSTARCH", runtime.GOARCH}, 42 {"GOHOSTOS", runtime.GOOS}, 43 {"GOOS", goos}, 44 {"GOPATH", os.Getenv("GOPATH")}, 45 {"GORACE", os.Getenv("GORACE")}, 46 {"GOROOT", goroot}, 47 {"GOTOOLDIR", toolDir}, 48 } 49 50 if goos != "plan9" { 51 cmd := b.gccCmd(".") 52 env = append(env, envVar{"CC", cmd[0]}) 53 env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")}) 54 } 55 56 if buildContext.CgoEnabled { 57 env = append(env, envVar{"CGO_ENABLED", "1"}) 58 } else { 59 env = append(env, envVar{"CGO_ENABLED", "0"}) 60 } 61 62 return env 63 } 64 65 func findEnv(env []envVar, name string) string { 66 for _, e := range env { 67 if e.name == name { 68 return e.value 69 } 70 } 71 return "" 72 } 73 74 func runEnv(cmd *Command, args []string) { 75 env := mkEnv() 76 if len(args) > 0 { 77 for _, name := range args { 78 fmt.Printf("%s\n", findEnv(env, name)) 79 } 80 return 81 } 82 83 switch runtime.GOOS { 84 default: 85 for _, e := range env { 86 fmt.Printf("%s=\"%s\"\n", e.name, e.value) 87 } 88 case "plan9": 89 for _, e := range env { 90 fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1)) 91 } 92 case "windows": 93 for _, e := range env { 94 fmt.Printf("set %s=%s\n", e.name, e.value) 95 } 96 } 97 }