通过interface {}传递的sql.NullFloat64上的调用方法抛出错误

I have a simple function that looks like this:

func convertToRealNum(number interface{}) interface{}{
  switch v := number.(type) { 
  default:
    log.Fatal("unexpected type %T", v)
  case sql.NullFloat64:
    newNumber := number.Float64
  case sql.NullInt64:
    newNumber := number.Int64
  }
  return newNumber 
}

number is either a NullFloat64 or a NullInt64. If number is the type NullFloat64, I call number.Float64 on it and get back the value of the number as a Float64. If I try to call the same thing inside a function that takes number as an argument that is an interface{} I get the compile error:

number.Float64 undefined (type interface {} has no field or method Float64)

Inside the function, if I call reflect.TypeOf(number) it'll return NullFloat64, so it knows what type it is, but I can't call that types methods.

The actual value of number (whatever type it is) is assigned to v in the type switch. The problem is you're re-using number in the assignment to newNumber, that is still the same old interface{} when you go into the case sql.NullFloat64 that is the type of v which has the value of number in it so you'll want to use it for assignment.

func convertToRealNum(number interface{}) interface{}{
 var newNumber interface{} 
 switch v := number.(type) { 
  default:
    log.Fatal("unexpected type %T", v)
  case sql.NullFloat64:
    newNumber = v.Float64
  case sql.NullInt64:
    newNumber = v.Int64
  }
  return newNumber  
}