unit.go 775 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // Use and distribution licensed under the Apache license version 2.
  3. //
  4. // See the COPYING file in the root project directory for full text.
  5. //
  6. package unitutil
  7. var (
  8. KB int64 = 1024
  9. MB = KB * 1024
  10. GB = MB * 1024
  11. TB = GB * 1024
  12. PB = TB * 1024
  13. EB = PB * 1024
  14. )
  15. // AmountString returns a string representation of the amount with an amount
  16. // suffix corresponding to the nearest kibibit.
  17. //
  18. // For example, AmountString(1022) == "1022). AmountString(1024) == "1KB", etc
  19. func AmountString(size int64) (int64, string) {
  20. switch {
  21. case size < MB:
  22. return KB, "KB"
  23. case size < GB:
  24. return MB, "MB"
  25. case size < TB:
  26. return GB, "GB"
  27. case size < PB:
  28. return TB, "TB"
  29. case size < EB:
  30. return PB, "PB"
  31. default:
  32. return EB, "EB"
  33. }
  34. }