🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ func Uint8FromInt(n int) (uint8, error) { if 0 <= n && n <= math.MaxUint8 { // conversion is safe return uint8(n), nil } return 0, fmt.Errorf("%d is out of the uint8 range", n) } ~~~ ~~~ func IntFromFloat64(x float64) int { if math.MinInt32 <= x && x <= math.MaxInt32 { // x lies in the integer range whole, fraction := math.Modf(x) if fraction >= 0.5 { whole++ } return int(whole) } panic(fmt.Sprintf("%g is out of the int32 range", x)) } ~~~ 终止程序 ~~~ if err != nil { fmt.Printf("Program stopping with error %v", err) os.Exit(1) } ~~~