windows.Environ() strings [0] and [1]

huangapple go评论91阅读模式
英文:

windows.Environ() strings [0] and [1]

问题

我对于在Windows 7系统上(使用go版本go1.8 windows/amd64)调用"windows.Environ()"函数返回的前两个字符串感到困惑。env[0]似乎具有键"=::";env1具有键"=C:"。有人可以告诉我这个在哪里有文档记录吗?提前谢谢。

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])
英文:

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 "=::"; env1 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])

答案1

得分: 2

相关的Go代码如下:

func Environ() []string {
    s, e := GetEnvironmentStrings()
    if e != nil {
        return nil
    }
    defer FreeEnvironmentStrings(s)
    r := make([]string, 0, 50) // 空的切片,有增长空间。
    for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
        if p[i] == 0 {
            // 空字符串标志着结束
            if i <= from {
                break
            }
            r = append(r, string(utf16.Decode(p[from:i])))
            from = i + 1
        }
    }
    return r
}

该代码使用了Windows的GetEnvironmentStrings函数。这些值来自于Windows环境变量。请参考Microsoft Windows文档中的环境变量部分。另外,还可以参考这篇文章中关于这些奇怪的=C:环境变量的解释。

英文:

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 &lt;&lt; 24]uint16)(unsafe.Pointer(s)); true; i++ {
		if p[i] == 0 {
			// empty string marks the end
			if i &lt;= 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?

huangapple
  • 本文由 发表于 2017年4月3日 08:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/43174876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定