| 1 |
// Copyright 2021 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. |
| 4 |
|
| 5 |
// Package godebug makes the simplified settings in the $GODEBUG environment variable |
| 6 |
// available to packages. |
| 7 |
// Needed since internal/godebug is not available here. |
| 8 |
package godebug |
| 9 |
|
| 10 |
import "os" |
| 11 |
|
| 12 |
func Get(key string) string { |
| 13 |
s := os.Getenv("GODEBUG") |
| 14 |
if s == "" { |
| 15 |
return "" |
| 16 |
} |
| 17 |
// Scan the string backward so that later settings are used |
| 18 |
// and earlier settings are ignored. |
| 19 |
// Note that a forward scan would cause cached values |
| 20 |
// to temporarily use the ignored value before being |
| 21 |
// updated to the "correct" one. |
| 22 |
end := len(s) |
| 23 |
eq := -1 |
| 24 |
for i := end - 1; i >= -1; i-- { |
| 25 |
if i == -1 || s[i] == ',' { |
| 26 |
if eq >= 0 { |
| 27 |
name, arg := s[i+1:eq], s[eq+1:end] |
| 28 |
if name == key { |
| 29 |
for j := 0; j < len(arg); j++ { |
| 30 |
if arg[j] == '#' { |
| 31 |
return arg[:j] |
| 32 |
} |
| 33 |
} |
| 34 |
return arg |
| 35 |
} |
| 36 |
} |
| 37 |
eq = -1 |
| 38 |
end = i |
| 39 |
} else if s[i] == '=' { |
| 40 |
eq = i |
| 41 |
} |
| 42 |
} |
| 43 |
return "" |
| 44 |
} |