1 // Copyright 2010 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.
5 // Windows environment variables.
14 func Getenv(key
string) (value
string, found
bool) {
15 keyp
, err
:= UTF16PtrFromString(key
)
21 b
:= make([]uint16, n
)
22 n
, err
= GetEnvironmentVariable(keyp
, &b
[0], uint32(len(b
)))
23 if n
== 0 && err
== ERROR_ENVVAR_NOT_FOUND
{
26 if n
<= uint32(len(b
)) {
27 return string(utf16
.Decode(b
[:n
])), true
32 func Setenv(key
, value
string) error
{
33 v
, err
:= UTF16PtrFromString(value
)
37 keyp
, err
:= UTF16PtrFromString(key
)
41 e
:= SetEnvironmentVariable(keyp
, v
)
48 func Unsetenv(key
string) error
{
49 keyp
, err
:= UTF16PtrFromString(key
)
53 return SetEnvironmentVariable(keyp
, nil)
57 for _
, s
:= range Environ() {
58 // Environment variables can begin with =
59 // so start looking for the separator = at j=1.
60 // http://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
61 for j
:= 1; j
< len(s
); j
++ {
70 func Environ() []string {
71 s
, e
:= GetEnvironmentStrings()
75 defer FreeEnvironmentStrings(s
)
76 r
:= make([]string, 0, 50) // Empty with room to grow.
77 for from
, i
, p
:= 0, 0, (*[1 << 24]uint16)(unsafe
.Pointer(s
)); true; i
++ {
79 // empty string marks the end
83 r
= append(r
, string(utf16
.Decode(p
[from
:i
])))