chore: change ifelse to switch

This commit is contained in:
suiyuan1314 2024-05-30 16:39:28 +08:00
parent 6bb13e8e2b
commit 1d4d7454f5

View file

@ -26,15 +26,16 @@ type StorageSize float64
// String implements the stringer interface. // String implements the stringer interface.
func (s StorageSize) String() string { func (s StorageSize) String() string {
if s > 1099511627776 { switch {
case s > 1099511627776:
return fmt.Sprintf("%.2f TiB", s/1099511627776) return fmt.Sprintf("%.2f TiB", s/1099511627776)
} else if s > 1073741824 { case s > 1073741824:
return fmt.Sprintf("%.2f GiB", s/1073741824) return fmt.Sprintf("%.2f GiB", s/1073741824)
} else if s > 1048576 { case s > 1048576:
return fmt.Sprintf("%.2f MiB", s/1048576) return fmt.Sprintf("%.2f MiB", s/1048576)
} else if s > 1024 { case s > 1024:
return fmt.Sprintf("%.2f KiB", s/1024) return fmt.Sprintf("%.2f KiB", s/1024)
} else { default:
return fmt.Sprintf("%.2f B", s) return fmt.Sprintf("%.2f B", s)
} }
} }
@ -42,15 +43,16 @@ func (s StorageSize) String() string {
// TerminalString implements log.TerminalStringer, formatting a string for console // TerminalString implements log.TerminalStringer, formatting a string for console
// output during logging. // output during logging.
func (s StorageSize) TerminalString() string { func (s StorageSize) TerminalString() string {
if s > 1099511627776 { switch {
case s > 1099511627776:
return fmt.Sprintf("%.2fTiB", s/1099511627776) return fmt.Sprintf("%.2fTiB", s/1099511627776)
} else if s > 1073741824 { case s > 1073741824:
return fmt.Sprintf("%.2fGiB", s/1073741824) return fmt.Sprintf("%.2fGiB", s/1073741824)
} else if s > 1048576 { case s > 1048576:
return fmt.Sprintf("%.2fMiB", s/1048576) return fmt.Sprintf("%.2fMiB", s/1048576)
} else if s > 1024 { case s > 1024:
return fmt.Sprintf("%.2fKiB", s/1024) return fmt.Sprintf("%.2fKiB", s/1024)
} else { default:
return fmt.Sprintf("%.2fB", s) return fmt.Sprintf("%.2fB", s)
} }
} }