package feed import ( "encoding/json" "fmt" "math" "strconv" ) // CVSS score type Score float32 // Unmarshal CVSS score from JSON. func (me *Score) UnmarshalJSON(b []byte) error { // decode float, check for error var v float32 if err := json.Unmarshal(b, &v); err != nil { return err } // check score if v < 0.0 || v > 10.0 { return fmt.Errorf("CVSS score out of bounds: %2.1f", v) } // save result, return success *me = Score(v) return nil } func (me Score) String() string { val := math.Trunc(10.0 * float64(me)) / 10.0 return strconv.FormatFloat(val, 'f', 1, 64) }