Golang从接口切片中删除nil {}

What is the best way to remove "nil" from slice of interface{} and generating a new slice of interface{}?

 Slice := []interface{}{1, nil, "string", nil}

Nothing good comes to my mind ?

newSlice := make([]interface{}, 0, len(Slice))
for _, item := range Slice {
    if item != nil {
        newSlice = append(newSlice, item)
    }
}

You can also use type switches like this example:

slice := []interface{}{1, nil, "string", nil}
newSlice := make([]interface{}, 0, len(slice))

for _, val := range(slice){
    switch val.(type) {
        case string, int: // add your desired types which will fill newSlice
            newSlice = append(newSlice, val)
    }
}

fmt.Printf("newSlice: %v\tType: %T
", newSlice, newSlice)

Output:

newSlice: [1 string]    Type: []interface {}

You can see the full example within The Go Playground