2014-07-15 2 views
2

저는 times, float32s 및 float64s를 처리하기 위해 타입 스위치를 사용하려고합니다. 시간과 float64에 대해서는 문제없이 작동하지만 strconv.FormatFloat(val, 'f', -1, 32)type float32type float64으로 사용할 수 없다고 계속 말합니다. 그 일이 어떻게 될지 모르겠다. 그래서 무언가를 놓치거나 내가 어떻게 부름을해야하는지 오해해야한다. FormatFloat()float32s.타입 스위치에 strconv.FormatFloat()를 사용하는 데 문제가 있습니다

func main() { 
    fmt.Println(test(rand.Float32())) 
    fmt.Println(test(rand.Float64())) 
} 

func test(val interface{}) string { 
    switch val := val.(type) { 
     case time.Time: 
      return fmt.Sprintf("%s", val) 
     case float64: 
      return strconv.FormatFloat(val, 'f', -1, 64) 
     case float32: 
      return strconv.FormatFloat(val, 'f', -1, 32) //here's the error 
     default: 
      return "Type not supported!" 
    } 
} 

오류 :

cannot use val (type float32) as type float64 in argument to strconv.FormatFloat

답변

6

FormatFloat의 첫 번째 인수는 float64 할 필요가, 당신은 float32를 전달하고 있습니다. 이 문제를 해결하는 가장 쉬운 방법은 32 비트 부동 소수점을 64 비트 부동 소수점으로 변환하는 것입니다.

case float32: 
    return strconv.FormatFloat(float64(val), 'f', -1, 32) 

http://play.golang.org/p/jBPaQ-jMBT

관련 문제