代码拉取完成,页面将自动刷新
package cvt
import (
"encoding/json"
"reflect"
"strconv"
"strings"
"time"
)
// Bool convert an interface to a bool type, with default value
func Bool(v interface{}, def ...bool) bool {
if v, err := BoolE(v); err == nil {
return v
}
if len(def) > 0 {
return def[0]
}
return false
}
// BoolP convert and store in a new bool value, and returns a pointer to it
func BoolP(v interface{}, def ...bool) *bool {
i := Bool(v, def...)
return &i
}
// BoolE convert an interface to a bool type
func BoolE(val interface{}) (bool, error) {
// direct type(for improve performance)
switch vv := val.(type) {
case nil:
return false, nil
case bool:
return vv, nil
case
float32, float64:
return Float64(vv) != 0, nil
case
time.Duration,
int, int8, int16, int32, int64:
return Int64(vv) != 0, nil
case uint, uint8, uint16, uint32, uint64:
return Uint64(vv) != 0, nil
case []byte:
return str2bool(string(vv))
case string:
return str2bool(vv)
case json.Number:
vvv, err := vv.Float64()
if err != nil {
return false, newErr(val, "bool")
}
return vvv != 0, nil
}
// indirect type
v, rv := Indirect(val)
switch vv := v.(type) {
case nil:
return false, nil
case bool:
return vv, nil
case int, int8, int16, int32, int64:
return rv.Int() != 0, nil
case uint, uint8, uint16, uint32, uint64:
return rv.Uint() != 0, nil
case float32, float64:
return rv.Float() != 0, nil
case []byte:
return str2bool(string(vv))
case string:
return str2bool(vv)
}
switch rv.Kind() {
// by elem length
case reflect.Array, reflect.Slice, reflect.Map:
return rv.Len() > 0, nil
}
return false, newErr(val, "bool")
}
// returns the boolean value represented by the string
func str2bool(str string) (bool, error) {
if val, err := strconv.ParseBool(str); err == nil {
return val, nil
} else if val, err := strconv.ParseFloat(str, 64); err == nil {
return val != 0, nil
}
switch strings.ToLower(strings.TrimSpace(str)) {
case "on", "yes", "y":
return true, nil
case "off", "no", "n":
return false, nil
default:
return false, newErr(str, "bool")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。