我试图弄清楚为什么我的代码不起作用。我希望取一片数字和字符串,并将其分成三片。对于切片中的每个元素,如果是字符串,则将其附加到字符串切片,如果是正数,则将其附加到正数,负数也一样。然而,这是输出
名称:EvTremblay 45.39934611083154 -75.71148292845268
[Crestview -75.73795670904249 BellevueManor -75.73886856878032 Dutchie'sHole -75.66809864107668 ...
正面:[45.344387632924054 45.37223315413918 ...] 负面:[]
这是我的代码。有人能告诉我是什么导致 Negatives 数组没有任何值吗?
func main() {
fmt.Printf("%q\n", strings.Split("a,b,c", ","))
var names []string
var positives, negatives []float64
bs, err := ioutil.ReadFile("poolss.txt")
if err != nil {
return
}
str := string(bs)
fmt.Println(str)
tokens := strings.Split(str, ",")
for _, token := range tokens {
if num, err := strconv.ParseFloat(token, 64); err == nil {
if num > 0 {
positives = append(positives, num)
} else {
negatives = append(negatives, num)
}
} else {
names = append(names, token)
}
fmt.Println(token)
}
fmt.Println(fmt.Sprintf("Strings: %v",names))
fmt.Println(fmt.Sprintf("Positives: %v", positives))
fmt.Println(fmt.Sprintf("Negatives: %v",negatives))
for i := range names{
fmt.Println(names[i])
fmt.Println(positives[i])
fmt.Println(negatives[i])
}
}