dongzhanbi0027 2016-11-30 22:37 采纳率: 0%
浏览 352

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 ?

  • 写回答

2条回答 默认 最新

  • duan4523 2016-11-30 22:41
    关注
    newSlice := make([]interface{}, 0, len(Slice))
    for _, item := range Slice {
        if item != nil {
            newSlice = append(newSlice, item)
        }
    }
    
    评论

报告相同问题?