“不能在赋值中用作类型字符串”

时间:2018-10-03 06:50:15

标签: go

我有以下字符串:

-1,856,32,0,0,0.000000,0.000000
0,0,0,137,0,0,0,140
0,0,101,0,0,0,42,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,554,0
-1,841,1,0,0,0.000000,0.000000
0,0,0,163,0,0,0,182
0,0,120,0,0,0,43,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,517,0

然后我使用分隔符-1对其进行拆分,这意味着将存在一个包含2个元素的数组(我们将其称为array1)。现在,让我们在array1的第一个元素上说一下,我想再次将其分解为\r\n,这将是一个由5个元素组成的数组(array2)。接下来,我想通过使用array2作为分隔符来拆分,的第一个元素,但这给了我一个警告:

cannot use strings.Split(d0, ",") (type []string) as type string in assignment

代码如下:

array1 := strings.Split(string(b), "-1,")
for k, chunk := range chunks{
    // skip the first
    // because it's blank
    // duno why
    if k == 0{
        continue
    }

    array2 := strings.Split(chunk, "\r\n")

    d0 := data[0]
    d0 = strings.Split(d0, ",") // this give me error: cannot use strings.Split(d0, ",") (type []string) as type string in assignment
    lat, lng := d0[4], d0[5]
    // 
    //  TODO: match lat and lng againts lamp table in DB which is now in $coords
    //  
    fmt.Println(lat, lng)

    data = data[1:]
    for _, v := range data{
        _ = v
    }
}

1 个答案:

答案 0 :(得分:0)

问题的答案在您指出的错误消息中:

  

不能使用strings.Split(d0,“,”)(类型[] string)作为分配中的类型字符串

原因是您声明if($(this).next(".tlists").is(":visible")) { $(this).next(".tlists").slideUp("slow"); $(this).children(".gold-ra").removeClass("down"); } else { $(this).next(".tlists").slideDown("slow"); $(this).children(".gold-ra").addClass("down"); } 的类型为“字符串”,然后尝试为其分配字符串的 slice 。由于这些类型不同,因此编译器会生成您列出的错误,并拒绝编译程序。

d0

要变通解决此问题,您只需声明一个新的字符串切片变量,而不要尝试重用名称d0 := data[0] // Here we declare d0 as a string and assign a value to it d0 = strings.Split(d0, ",") // ERROR: "Split" returns a []string but d0 is a string!

d0