从切片中删除多个项目

And*_*huk 2 go slice

我正在尝试使用此处的删除示例从切片中删除多个项目:http://code.google.com/p/go-wiki/wiki/SliceTricks 以下是我的代码:

package main

import "fmt"
import "net"

func main() {
    a := []string{"72.14.191.202", "69.164.200.202", "72.14.180.202", "2600:3c00::22", "2600:3c00::32", "2600:3c00::12"}
    fmt.Println(a)
    for index, element := range a {
        if net.ParseIP(element).To4() == nil {
            //a = append(a[:index], a[index+1:]...)
            a = a[:index+copy(a[index:], a[index+1:])]
        }
    }
    fmt.Println(a)
}
Run Code Online (Sandbox Code Playgroud)

虽然如果切片中只有一个IPv6地址,代码工作正常,但如果有多个IPv6地址则会失败.它失败并显示错误"panic:runtime error:slice bounds out of range".我该怎么做才能修复此代码,以便能够删除所有IPv6地址?

ANi*_*sus 10

您的问题是您正在修改正在迭代的切片.下面是你的代码有点修改:

package main

import (
    "fmt"
    "net"
)

func main() {
    a := []string{"72.14.191.202", "69.164.200.202", "72.14.180.202", "2600:3c00::22", "2600:3c00::32", "2600:3c00::12"}
    fmt.Println(a)
    for i := 0; i < len(a); i++ {
        if net.ParseIP(a[i]).To4() == nil {
            a = append(a[:i], a[i+1:]...)
            //a = a[:i+copy(a[i:], a[i+1:])]
            i-- // Since we just deleted a[i], we must redo that index
        }
    }
    fmt.Println(a)
}
Run Code Online (Sandbox Code Playgroud)

操场


LeG*_*GEC 5

只是提出一点:改变正在迭代的结构总是很棘手的。
避免这种情况的常见方法是在新变量中构建最终结果:

package main

import (
    "fmt"
    "net"
)

func main() {
    a := []string{"72.14.191.202", "69.164.200.202", "72.14.180.202", "2600:3c00::22", "2600:3c00::32", "2600:3c00::12"}
    fmt.Println(a)

    var b []string
    for _, ip := range a {
        if net.ParseIP(ip).To4() != nil {
                b = append(b, ip)
        }
    }
    fmt.Println(b)
}
Run Code Online (Sandbox Code Playgroud)

http://play.golang.org/p/7CLMPw_FQi