fmt.println(ParseSize("200GB")) =214748364800,200GB
代码如下:
package cache
import (
"log"
"regexp"
"strconv"
"strings"
)
const (
B = 1 << (iota * 10)
KB
MB
GB
TB
PB
)
func ParseSize(size string) (int64, string) {
//默认大小为100MB
re, _ := regexp.Compile("[0-9]+")
unit := string(re.ReplaceAll([]byte(size), []byte("")))
num, _ := strconv.ParseInt(strings.Replace(size, unit, "", 1), 10, 64)
unit = strings.ToUpper(unit)
var byteNum int64 = 0
switch unit {
case "B":
byteNum = num
case "KB":
byteNum = num * KB
case "MB":
byteNum = num * MB
case "GB":
byteNum = num * GB
case "TB":
byteNum = num * TB
case "PB":
byteNum = num * PB
default:
num = 0
byteNum = 0
}
if num == 0 {
log.Println("ParseSize 仅支持B KB MB GB TB PB")
num = 100
unit = "MB"
byteNum = num * MB
}
sizeStr := strconv.FormatInt(num, 10) + unit
return byteNum, sizeStr
}