【问题标题】:windows.Environ() strings [0] and [1]windows.Environ() 字符串 [0] 和 [1]
【发布时间】:2017-08-27 17:51:15
【问题描述】:

我对 Windows pro 7 系统(go 版本 go1.8 windows/amd64)上“windows.Environ()”返回的前 2 个字符串感到困惑。 env[0] 显然有一个键 "=::"; env[1] 有一个键“=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])

【问题讨论】:

  • windows 包/变量是什么?请出示您的完整源代码。
  • @Flimzy:你应该知道:godoc.org/golang.org/x/sys/windows#Environ
  • @peterSO:我为什么要知道?它不是标准库的一部分。
  • 你在使用x/sys/windows吗?如果是这样,为什么? “这个包的主要用途是在为系统提供更便携接口的其他包中,例如“os”、“time”和“net”。如果可以,请使用这些包而不是这个包。 os.Env 对你不起作用有什么原因吗?
  • @Flimzy: golang.org/x/sys/windows 是官方的 Go 包,它是依赖于操作系统的 sycall 包的扩展。如果您要回答与 Go 操作系统相关的问题,您应该知道这一点。尤其是当您投反对票或投票结束时。

标签: windows go environment-variables standard-library


【解决方案1】:

相关的Go代码是:

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
}

代码正在使用 Windows GetEnvironmentStrings 函数。这些值来自 Windows。请参阅环境变量的 Microsoft Windows 文档。另见What are these strange =C: environment variables?

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多