本文整理汇总了Golang中reflect.Value.SetFloat方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetFloat方法的具体用法?Golang Value.SetFloat怎么用?Golang Value.SetFloat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.SetFloat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: submitProperty
func (db *DataBinder) submitProperty(prop Property, field reflect.Value) error {
value := prop.Get()
if value == nil {
// This happens e.g. if CurrentIndex() of a ComboBox returns -1.
// FIXME: Should we handle this differently?
return nil
}
if err, ok := value.(error); ok {
return err
}
if f64, ok := value.(float64); ok {
switch field.Kind() {
case reflect.Float32, reflect.Float64:
field.SetFloat(f64)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(int64(f64))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
field.SetUint(uint64(f64))
default:
return newError(fmt.Sprintf("Field '%s': Can't convert float64 to %s.", prop.Source().(string), field.Type().Name()))
}
return nil
}
field.Set(reflect.ValueOf(value))
return nil
}
示例2: setVal
func (tbl *Table) setVal(field *reflect.Value, val interface{}) {
str, ok := val.(string)
log.Println("trying to set")
if ok {
log.Println("setting string")
field.SetString(str)
return
}
i, ok := val.(int64)
if ok {
field.SetInt(i)
return
}
ui, ok := val.(uint64)
if ok {
field.SetUint(ui)
return
}
b, ok := val.(bool)
if ok {
field.SetBool(b)
return
}
f, ok := val.(float64)
if ok {
field.SetFloat(f)
return
}
t, ok := val.(time.Time)
if ok {
field.Set(reflect.ValueOf(t))
return
}
}
示例3: setFieldDefaults
func setFieldDefaults(v reflect.Value, sf reflect.StructField, s reflect.Value) {
if v.CanInterface() && reflect.DeepEqual(
v.Interface(), reflect.Zero(v.Type()).Interface()) {
tag := sf.Tag.Get("ebmldef")
if tag != "" {
switch v.Kind() {
case reflect.Int, reflect.Int64:
u, _ := strconv.ParseInt(tag, 10, 0)
v.SetInt(int64(u))
case reflect.Uint, reflect.Uint64:
u, _ := strconv.ParseUint(tag, 10, 0)
v.SetUint(u)
case reflect.Float32, reflect.Float64:
f, _ := strconv.ParseFloat(tag, 64)
v.SetFloat(f)
case reflect.String:
v.SetString(tag)
default:
log.Panic("Unsupported default value")
}
}
ltag := sf.Tag.Get("ebmldeflink")
if ltag != "" {
v.Set(s.FieldByName(ltag))
}
}
}
示例4: readField
func (e *Element) readField(v reflect.Value) (err error) {
switch v.Kind() {
case reflect.Struct:
err = e.readStruct(v)
case reflect.Slice:
err = e.readSlice(v)
case reflect.Array:
for i, l := 0, v.Len(); i < l && err == nil; i++ {
err = e.readStruct(v.Index(i))
}
case reflect.String:
var s string
s, err = e.readString()
v.SetString(s)
case reflect.Int, reflect.Int64:
var u uint64
u, err = e.readUint64()
v.SetInt(int64(u))
case reflect.Uint, reflect.Uint64:
var u uint64
u, err = e.readUint64()
v.SetUint(u)
case reflect.Float32, reflect.Float64:
var f float64
f, err = e.readFloat()
v.SetFloat(f)
default:
err = errors.New("Unknown type: " + v.String())
}
return
}
示例5: setValue
// setValue updates the field value in fv to the data extracted from config
// with key.
func setValue(fv *reflect.Value, c *Config, key string) error {
// Update the value with the correct type
switch fv.Kind() {
case reflect.Int:
val, err := c.GetInt(key)
if err != nil {
return err
}
fv.SetInt(int64(val))
case reflect.Float64:
val, err := c.GetFloat(key)
if err != nil {
return err
}
fv.SetFloat(val)
case reflect.Bool:
val, err := c.GetBool(key)
if err != nil {
return err
}
fv.SetBool(val)
case reflect.String:
val, err := c.GetString(key)
if err != nil {
return err
}
fv.SetString(val)
}
return nil
}
示例6: setWithProperType
// setWithProperType sets proper value to field based on its type,
// but it does not return error for failing parsing,
// because we want to use default value that is already assigned to strcut.
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string) error {
switch t.Kind() {
case reflect.String:
if len(key.String()) == 0 {
return nil
}
field.SetString(key.String())
case reflect.Bool:
boolVal, err := key.Bool()
if err != nil {
return nil
}
field.SetBool(boolVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
durationVal, err := key.Duration()
// Skip zero value
if err == nil && int(durationVal) > 0 {
field.Set(reflect.ValueOf(durationVal))
return nil
}
intVal, err := key.Int64()
if err != nil || intVal == 0 {
return nil
}
field.SetInt(intVal)
// byte is an alias for uint8, so supporting uint8 breaks support for byte
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
durationVal, err := key.Duration()
// Skip zero value
if err == nil && int(durationVal) > 0 {
field.Set(reflect.ValueOf(durationVal))
return nil
}
uintVal, err := key.Uint64()
if err != nil {
return nil
}
field.SetUint(uintVal)
case reflect.Float64:
floatVal, err := key.Float64()
if err != nil {
return nil
}
field.SetFloat(floatVal)
case reflectTime:
timeVal, err := key.Time()
if err != nil {
return nil
}
field.Set(reflect.ValueOf(timeVal))
case reflect.Slice:
return setSliceWithProperType(key, field, delim)
default:
return fmt.Errorf("unsupported type '%s'", t)
}
return nil
}
示例7: setConfigVal
func setConfigVal(str string, field reflect.Value) error {
t := field.Kind()
switch {
case t == reflect.String:
field.SetString(str)
case t >= reflect.Int && t <= reflect.Int64:
if x, err := strconv.ParseInt(str, 10, 64); err != nil {
return err
} else {
field.SetInt(x)
}
case t >= reflect.Uint && t <= reflect.Uint64:
if x, err := strconv.ParseUint(str, 10, 64); err != nil {
return err
} else {
field.SetUint(x)
}
case t >= reflect.Float32 && t <= reflect.Float64:
if x, err := strconv.ParseFloat(str, 64); err != nil {
return err
} else {
field.SetFloat(x)
}
case t == reflect.Bool:
if x, err := strconv.ParseBool(str); err != nil {
return err
} else {
field.SetBool(x)
}
default:
return nil
}
return nil
}
示例8: decodeField
func (d *Decoder) decodeField(v reflect.Value, val string) error {
switch v.Type() {
case reflect.TypeOf(time.Duration(0)):
a, err := time.ParseDuration(val)
if err != nil {
return err
}
v.SetInt(int64(a))
default:
switch v.Kind() {
case reflect.String:
v.SetString(val)
case reflect.Int, reflect.Int32, reflect.Int64:
v.Type()
nv, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
v.SetInt(nv)
case reflect.Bool:
b, err := strconv.ParseBool(val)
if err != nil {
return err
}
v.SetBool(b)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
v.SetFloat(f)
}
}
return nil
}
示例9: readFloat
func (decoder *Decoder) readFloat(value reflect.Value) error {
bytes, err := decoder.readBytes(8)
if err != nil {
return err
}
n := uint64(0)
for _, b := range bytes {
n <<= 8
n |= uint64(b)
}
v := math.Float64frombits(n)
switch value.Kind() {
case reflect.Float32, reflect.Float64:
value.SetFloat(v)
case reflect.Int32, reflect.Int, reflect.Int64:
value.SetInt(int64(v))
case reflect.Uint32, reflect.Uint, reflect.Uint64:
value.SetUint(uint64(v))
case reflect.Interface:
value.Set(reflect.ValueOf(v))
default:
return errors.New("invalid type:" + value.Type().String() + " for double")
}
return nil
}
示例10: setWithProperType
// This sets the value in a struct of an indeterminate type to the
// matching value from the request (via Form middleware) in the
// same type, so that not all deserialized values have to be strings.
// Supported types are string, int, float, and bool.
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
if scan, ok := structField.Addr().Interface().(sql.Scanner); ok {
scan.Scan(val)
return
}
switch valueKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if val == "" {
val = "0"
}
intVal, err := strconv.ParseInt(val, 10, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as integer")
} else {
structField.SetInt(intVal)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if val == "" {
val = "0"
}
uintVal, err := strconv.ParseUint(val, 10, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as unsigned integer")
} else {
structField.SetUint(uintVal)
}
case reflect.Bool:
if val == "" {
val = "false"
}
boolVal, err := strconv.ParseBool(val)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as boolean")
} else {
structField.SetBool(boolVal)
}
case reflect.Float32:
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, 32)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 32-bit float")
} else {
structField.SetFloat(floatVal)
}
case reflect.Float64:
if val == "" {
val = "0.0"
}
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 64-bit float")
} else {
structField.SetFloat(floatVal)
}
case reflect.String:
structField.SetString(val)
}
}
示例11: setFieldVal
// setFieldVal first converts v to the type of structField, then uses reflection
// to set the field to the converted value.
func setFieldVal(structField reflect.Value, name string, v string) error {
switch structField.Kind() {
case reflect.String:
structField.SetString(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vInt, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetInt(int64(vInt))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vUint, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetUint(uint64(vUint))
case reflect.Float32, reflect.Float64:
vFloat, err := strconv.ParseFloat(v, 64)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetFloat(vFloat)
case reflect.Bool:
vBool, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
}
structField.SetBool(vBool)
default:
return fmt.Errorf("envvar: Unsupported struct field type: %s", structField.Type().String())
}
return nil
}
示例12: processField
func processField(value string, field reflect.Value) error {
typ := field.Type()
decoder := decoderFrom(field)
if decoder != nil {
return decoder.Decode(value)
}
switch typ.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var (
val int64
err error
)
if field.Kind() == reflect.Int64 && typ.PkgPath() == "time" && typ.Name() == "Duration" {
var d time.Duration
d, err = time.ParseDuration(value)
val = int64(d)
} else {
val, err = strconv.ParseInt(value, 0, typ.Bits())
}
if err != nil {
return err
}
field.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := strconv.ParseUint(value, 0, typ.Bits())
if err != nil {
return err
}
field.SetUint(val)
case reflect.Bool:
val, err := strconv.ParseBool(value)
if err != nil {
return err
}
field.SetBool(val)
case reflect.Float32, reflect.Float64:
val, err := strconv.ParseFloat(value, typ.Bits())
if err != nil {
return err
}
field.SetFloat(val)
case reflect.Slice:
vals := strings.Split(value, ",")
sl := reflect.MakeSlice(typ, len(vals), len(vals))
for i, val := range vals {
err := processField(val, sl.Index(i))
if err != nil {
return err
}
}
field.Set(sl)
}
return nil
}
示例13: setWithProperType
// setWithProperType sets proper value to field based on its type,
// but it does not return error for failing parsing,
// because we want to use default value that is already assigned to strcut.
func setWithProperType(kind reflect.Kind, key *Key, field reflect.Value, delim string) error {
switch kind {
case reflect.String:
if len(key.String()) == 0 {
return nil
}
field.SetString(key.String())
case reflect.Bool:
boolVal, err := key.Bool()
if err != nil {
return nil
}
field.SetBool(boolVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intVal, err := key.Int64()
if err != nil {
return nil
}
field.SetInt(intVal)
case reflect.Float64:
floatVal, err := key.Float64()
if err != nil {
return nil
}
field.SetFloat(floatVal)
case reflectTime:
timeVal, err := key.Time()
if err != nil {
return nil
}
field.Set(reflect.ValueOf(timeVal))
case reflect.Slice:
vals := key.Strings(delim)
numVals := len(vals)
if numVals == 0 {
return nil
}
sliceOf := field.Type().Elem().Kind()
var times []time.Time
if sliceOf == reflectTime {
times = key.Times(delim)
}
slice := reflect.MakeSlice(field.Type(), numVals, numVals)
for i := 0; i < numVals; i++ {
switch sliceOf {
case reflectTime:
slice.Index(i).Set(reflect.ValueOf(times[i]))
default:
slice.Index(i).Set(reflect.ValueOf(vals[i]))
}
}
field.Set(slice)
default:
return fmt.Errorf("unsupported type '%s'", kind)
}
return nil
}
示例14: numberDecoder
func numberDecoder(node RMNode, data interface{}, v reflect.Value) {
val, err := redis.String(data, nil)
if err != nil {
panic(err)
}
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(val, 10, 64)
if err != nil || v.OverflowInt(n) {
panic(fmt.Sprintf("Unable to convert int: %s \n", val))
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(val, 10, 64)
if err != nil || v.OverflowUint(n) {
panic(fmt.Sprintf("Unable to convert uint: %s \n", val))
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(val, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
panic(fmt.Sprintf("Unable to convert float: %s \n", val))
}
v.SetFloat(n)
default:
panic(fmt.Sprintf("Unsupported number convertion for type[%v] with value[%v]", v.Type(), data))
}
}
示例15: double
// type(1) | name length(1) | raw name bytes | 0x00 | value bytes(8)
func (d *decodeState) double(v reflect.Value) {
d.off += 1 // type
klen := int(Int8(d.data[d.off:]))
d.off += 1 // name length
if klen > 0 {
key := d.data[d.off : d.off+klen-1]
d.off += klen
v = fieldByTag(v, key)
u, pv := d.indirect(v, false)
if u != nil {
d.off -= 1 + 1 + klen
if err := u.UnmarshalMCPACK(d.next()); err != nil {
d.error(err)
}
return
}
v = pv
}
val := Float64(d.data[d.off:])
d.off += 8
if !v.IsValid() {
return
}
v.SetFloat(val)
}