duanlu6114 2017-04-03 00:08
浏览 65
已采纳

windows.Environ()字符串[0]和[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'
",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'
",str_EnvStrs[1])
  • 写回答

1条回答 默认 最新

  • doucheng1891 2017-04-03 01:39
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?