1 Star 1 Fork 0

lihao1988 / go-array

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

go-array

MIT licensed

介绍

基于 reflect 的应用,实现 go 语言 Array(Slice/Map) 常用工具函数,具体的函数方式下面有详细内容讲解。

安装

// github
go get github.com/lihao1988/go-array

// gitee
go get gitee.com/lihao1988/go-array

版本要求

Go 1.15 or above.

Array(Slice/Map) Functions

In(value, array interface{}) bool

// 用于检查Array(Slice/Map)中是否存在指定的值
value = "a"
dataMap := []string{"a", "b", "c"}
isExist := In(value, dataMap)
fmt.Println(isExist)

Keys(array interface{}, &keys interface{})

// 获取数组中所有的键名
dataMap := map[string]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
}

var keys []string
Keys(dataMap, &keys)
fmt.Println(keys)

Values(array interface{}, &values interface{})

// 获取数组中所有的值
dataMap := map[string]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
}

var values []string
Values(dataMap, &values)
fmt.Println(values)

Merge(&array interface{}, dArr ...interface{})

// 把一个或多个数组合并为一个数组
dataMap := map[string]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
}

dataOne := map[string]string{
    "c": "this is 'C'",
}
dataTwo := map[string]string{
    "d": "this is 'D'",
}

Merge(&dataMap, dataOne, dataTwo)
fmt.Println(dataMap)

Unique(&array interface{})

// 删除数组中的重复值
dataMap := map[int]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
    "a": "this is 'A'",
}
Unique(&dataMap)
fmt.Println(dataMap)

Column(&dest, input interface{}, columnKey, indexKey string)

// 返回输入数组中某个单一列的值
dataMap := map[string]map[string]string{
    "a": {"key": "a1", "value": "A"},
    "b": {"key": "b1", "value": "B"},
}

// key 使用子数组字段 'key',column 为子数组内容
dest := map[string]map[string]string{}
Column(&dest, dataMap, "", "key")
fmt.Println(dest)

//key 使用子数组字段 'key',column 使用子数组字段 'value'
dest := map[string]string{}
Column(&dest, dataMap, "value", "key")
fmt.Println(dest)

//key 为空则为 list,column 使用子数组字段 'value'
dest := []map[string]string{}
Column(&dest, dataMap, "value", "")
fmt.Println(dest)

Diff(&array interface{}, dArr ...interface{})

// 比较数组,返回差集(只比较键值)
dataMap := map[int]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
	"c": "this is 'C'",
}

dataOne := map[int]string{
    "b": "this is 'B'",
    "d": "this is 'D'",
}

dataTwo := map[int]string{
    "c": "this is 'C'",
    "e": "this is 'E'",
}

Diff(&dataMap, dataOne, dataTwo)
fmt.Println("dataMap", dataMap)

Intersect(&array interface{}, dArr ...interface{})

// 比较数组,返回交集(只比较键值)
dataMap := map[int]string{
    "a": "this is 'A'",
    "b": "this is 'B'",
}

dataOne := map[int]string{
    "b": "this is 'B'",
    "d": "this is 'D'",
}

dataTwo := map[int]string{
    "c": "this is 'C'",
    "e": "this is 'E'",
}

Intersect(&dataMap, dataOne, dataTwo)
fmt.Println("dataMap", dataMap)
MIT License Copyright (c) 2023 breeze Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

实现 go 语言 Array(Slice/Map) 常用工具函数 展开 收起
Go
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/lihao1988/go-array.git
git@gitee.com:lihao1988/go-array.git
lihao1988
go-array
go-array
master

搜索帮助