暫無描述

type_assertion.go 361B

123456789101112131415161718192021
  1. package main
  2. // TYPE ASSERTION OMIT
  3. func NewtonSqrt[T ~float32 | ~float64 ](v T) T {
  4. var iterations int
  5. switch (interface{})(v).(type) {
  6. case float32:
  7. iterations = 4
  8. case float64:
  9. iterations = 5
  10. default:
  11. panic(fmt.Sprintf("unexpected type %T", v))
  12. }
  13. // Code omitted.
  14. }
  15. type MyFloat float32
  16. var G = NewtonSqrt(MyFloat(64))
  17. // TYPE ASSERTION OMIT