You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scan.go 623B

1234567891011121314151617181920212223
  1. package types
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. )
  7. // ScanFully uses fmt.Sscanf with verb to fully scan val into ptr.
  8. func ScanFully(ptr interface{}, val string, verb byte) error {
  9. t := reflect.ValueOf(ptr).Elem().Type()
  10. // attempt to read extra bytes to make sure the value is consumed
  11. var b []byte
  12. n, err := fmt.Sscanf(val, "%"+string(verb)+"%s", ptr, &b)
  13. switch {
  14. case n < 1 || n == 1 && err != io.EOF:
  15. return fmt.Errorf("failed to parse %q as %v: %v", val, t, err)
  16. case n > 1:
  17. return fmt.Errorf("failed to parse %q as %v: extra characters %q", val, t, string(b))
  18. }
  19. // n == 1 && err == io.EOF
  20. return nil
  21. }