func Parser_Scell(str string) ( float64, error) {
re, err := regexp.Compile(SCells\s+([0-9.]+\s([MK]bps)*).)
if !re.MatchString(str) {
err = errors.New(“error to macth the string”)
log.Println(“error to match: “, err)
return 0, err
}
m := re.FindStringSubmatch(str)
bp := m[0]
re1 := strings.TrimLeft(bp, “SCells “)
log.Println(re1)
var re2 string
if strings.Contains(re1, “bps”) {
re2 = strings.TrimRight(re1, “ Mbps”)
log.Println(re2)
} else {
re2 = “0”
}
tputs, errs := strconv.ParseFloat(re2, 32)
if errs != nil {
log.Println(“parse string to float error: “, errs)
return 0, errs
}
return tputs, nil
}
func Parser_Pcell(str string) ( float64, error) {
re, err := regexp.Compile(PCell\s+([0-9.]+\s([MK]bps)*).)
if !re.MatchString(str) {
err = errors.New(“error to macth the string”)
log.Println(“error to match: “, err)
return 0, err
}
m := re.FindStringSubmatch(str)
bp := m[0]
re1 := strings.TrimLeft(bp, “PCell “)
log.Println(re1)
var re2 string
if strings.Contains(re1, “bps”) {
re2 = strings.TrimRight(re1, “ Mbps”)
log.Println(re2)
} else {
re2 = “0”
}
tputs, errs := strconv.ParseFloat(re2, 32)
if errs != nil {
log.Println(“parse string to float error: “, errs)
return 0, errs
}
return tputs, nil
}
想改成
func Parser(flag int,str string) ( float64, error) {
if flag == 0{
re, err := regexp.Compile(SCells\s+([0-9.]+\s([MK]bps)*).)
} else if flag ==1 {
re, err := regexp.Compile(SCells\s+([0-9.]+\s([MK]bps)*).)
}
if !re.MatchString(str) {
err = errors.New(“error to macth the string”)
log.Println(“error to match: “, err)
return 0, err
}
m := re.FindStringSubmatch(str)
bp := m[0]
re1 := strings.TrimLeft(bp, “SCells “)
log.Println(re1)
var re2 string
if strings.Contains(re1, “bps”) {
re2 = strings.TrimRight(re1, “ Mbps”)
log.Println(re2)
} else {
re2 = “0”
}
tputs, errs := strconv.ParseFloat(re2, 32)
if errs != nil {
log.Println(“parse string to float error: “, errs)
return 0, errs
}
return tputs, nil
}