📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

parseFloat64

Golang: Convert interface{} to float64.

import (
	"fmt"
	"math"
)

func parseFloat64(unk interface{}) (float64, error) {
	switch i := unk.(type) {
	case float64:
		return i, nil
	case float32:
		return float64(i), nil
	case int64:
		return float64(i), nil
	case int32:
		return float64(i), nil
	case int:
		return float64(i), nil
	case uint64:
		return float64(i), nil
	case uint32:
		return float64(i), nil
	case uint:
		return float64(i), nil
	default:
		return math.NaN(), fmt.Errorf("failed to parse to float")
	}
}