-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool.go
52 lines (46 loc) · 1.14 KB
/
bool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package g
import (
"errors"
"strings"
)
// StringToBool converts a string to a boolean.
// It handles various string representations of boolean values such as
// "true", "false", "yes", "no", "on", "off". If the conversion fails
// and a default value is provided, it returns the default value. Otherwise,
// it returns an error.
//
// Example Usage:
//
// b, err := StringToBool("true") // true, nil
// b, err := StringToBool("yes") // true, nil
// b, err := StringToBool("abc", false) // false, error
func StringToBool(v string, def ...bool) (bool, error) {
var d bool = false
if len(def) > 0 {
d = def[0]
}
v = strings.ToLower(v)
switch v {
case "true", "yes", "on":
return true, nil
case "false", "no", "off":
return false, nil
case "":
return d, errors.New("empty string and no default value")
default:
return d, errors.New("invalid boolean string")
}
}
// BoolToString converts a boolean to a string.
// It returns "true" for true and "false" for false.
//
// Example Usage:
//
// s := BoolToString(true) // "true"
// s := BoolToString(false) // "false"
func BoolToString(b bool) string {
if b {
return "true"
}
return "false"
}