-1

I'm confused by the first 2 strings returned by "windows.Environ()" on a Windows pro 7 system (go version go1.8 windows/amd64). env[0] apparently has a key of "=::"; env[1] has a key "=C:". Can anyone point me to where this is documented? Thx in advance.

str_EnvStrs := windows.Environ()
// 
//    str_EnvStrs[0] == '=::=::\'
fmt.Printf("str_EnvStrs[0] == '%v'\n",str_EnvStrs[0])
//
//    str_EnvStrs[1] == '=C:=C:\Users\(WINLOGIN)\Documents\Source\go\src 
//                       \github.com\(GITLOGIN)\maps_arrays_slices'
fmt.Printf("str_EnvStrs[1] == '%v'\n",str_EnvStrs[1])
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mnemotronic
  • 856
  • 1
  • 9
  • 16
  • 1
    What is the `windows` package/variable? Please show your complete source code. – Jonathan Hall Apr 03 '17 at 00:22
  • @Flimzy: You should know that: https://godoc.org/golang.org/x/sys/windows#Environ – peterSO Apr 03 '17 at 00:37
  • 3
    @peterSO: Why should I know that? It's not part of the standard library. – Jonathan Hall Apr 03 '17 at 00:37
  • Are you using `x/sys/windows`? If so, why? "The primary use of this package is inside other packages that provide a more portable interface to the system, such as "os", "time" and "net". Use those packages rather than this one if you can." Is there a reason `os.Env` doesn't work for you? – Jonathan Hall Apr 03 '17 at 00:44
  • @Flimzy: `golang.org/x/sys/windows` is an official Go package, it's an extension of the OS dependent `sycall` package. If you are going to answer Go OS dependent questions you should know that. Especially if you downvote or vote to close. – peterSO Apr 03 '17 at 01:19

1 Answers1

2

The relevant Go code is:

func Environ() []string {
    s, e := GetEnvironmentStrings()
    if e != nil {
        return nil
    }
    defer FreeEnvironmentStrings(s)
    r := make([]string, 0, 50) // Empty with room to grow.
    for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
        if p[i] == 0 {
            // empty string marks the end
            if i <= from {
                break
            }
            r = append(r, string(utf16.Decode(p[from:i])))
            from = i + 1
        }
    }
    return r
}

The code is using the Windows GetEnvironmentStrings function. The values come from Windows. See the Microsoft Windows documentation of environment variables. Also, see What are these strange =C: environment variables?

peterSO
  • 158,998
  • 31
  • 281
  • 276