Golang对元素slice并去重


参考博客:

https://blog.csdn.net/qq_27068845/article/details/77407358

封装的类似PHP的array_column

// ArrayColumn 获取二维数组某一个键值
func ArrayColumn(input []map[string]interface{},columnKey string) ([]interface{}){
    if len(input)==0 {
        return []interface{}{}
    }
    result := []interface{}{}
    // for i := 0; i < len(input); i++ {
    //     result = append(result, input[i][columnKey])
    // }
    for _,v := range input {
        fmt.Print(v[columnKey])
        result = append(result, v[columnKey])
    }
    return result
}

// ArrayUnique 数组去重
func ArrayUnique(input []string)([]string){
    result := []string{}
    // 存放不重复主键
    tempMap := map[string]byte{}
    for _, e := range input{
        l := len(tempMap)
        tempMap[e] = 0
        if len(tempMap) != l{
            // 加入map后,map长度变化,则元素不重复
            result = append(result, e)
        }
    }
    return result    
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM