feat(Go-Tool):添加FileUtil.go、FileReadTool.go单测类代码,修改相应bug-----IniTool.go依赖获取不到,注释掉代码、创建ArrayUtil.go,添加数组对应方法

This commit is contained in:
huangzj
2020-04-30 18:08:29 +08:00
parent 9549597abc
commit 62266cd26b
16 changed files with 660 additions and 153 deletions
+131
View File
@@ -0,0 +1,131 @@
/*
* @Author : huangzj
* @Time : 2020/4/30 15:53
* @Description: 数组工具
* 这边测试了一下,go的结构体或者是非结构体类型,通过==比较的话,会校验到对应的每一个属性的值。
* 也就是说就算两个结构体的指针地址不一样,但是他的所有属性值都是一样的话,==返回的结果也是true
* 这边直接在入口的地方进行了map、slice、interface、Func、chan、ptr、UnsafePointer 类型的拦截
* 但是如果结构体中包含不能比较的类型,应该也是会报错
*/
package array
import "reflect"
var (
invalid = [7]reflect.Kind{reflect.UnsafePointer, reflect.Map, reflect.Slice,
reflect.Interface, reflect.Func, reflect.Chan, reflect.Ptr}
)
/*
* 返回数组中是否包含对应元素的结果
*/
func Contains(list []interface{}, ele interface{}) bool {
if typeInvalid(reflect.TypeOf(ele).Kind()) {
panic("该类型不支持比较")
}
for _, row := range list {
if row == ele {
return true
}
}
return false
}
/*
* 返回数组中是否不包含对应元素的结果
*/
func NotContains(list []interface{}, ele interface{}) bool {
return !Contains(list, ele)
}
/*
* 返回两个数组的所有元素是否相等的结果
*/
func IsSameList(list, eList []interface{}) bool {
same := 0
if len(list) != len(eList) {
return false
}
for _, l := range list {
for _, r := range eList {
if l == r {
same++
break
}
}
}
return same == len(list)
}
/*
* @param fList 传父集合
* @param sList 传子集合
* @return sList是不是fList的子集
*/
func IsSubSet(fList, sList []interface{}) bool {
if len(fList) < len(sList) {
return false
}
for _, r := range sList {
if NotContains(fList, r) {
return false
}
}
return true
}
/*
* 获取两个集合的交集
*/
func Intersection(list, otherList []interface{}) []interface{} {
result := make([]interface{}, 0)
for _, row := range list {
if Contains(otherList, row) {
result = append(result, row)
}
}
return result
}
/*
* 返回前一个数组包含,后一个数组不包含的结果(不是数量大的数组-数量小的数组)
*/
func DiffSet(cList, nList []interface{}) []interface{} {
result := make([]interface{}, 0)
for _, row := range cList {
if NotContains(nList, row) {
result = append(result, row)
}
}
return result
}
/*
* 返回两个数组的并集,不包含重复的数据
*/
func Union(aList, bList []interface{}) []interface{} {
result := make([]interface{}, 0)
for _, row := range aList {
if NotContains(result, row) {
result = append(result, row)
}
}
for _, row := range bList {
if NotContains(bList, row) {
result = append(result, row)
}
}
return result
}
func typeInvalid(k reflect.Kind) bool {
for _, row := range invalid {
if k == row {
return true
}
}
return false
}
+44
View File
@@ -66,3 +66,47 @@ func ReadByCircle(filePath string) (string, *err2.FileError) {
return fs, nil
}
func ReadFileLineNum(filePath string) (int, *err2.FileError) {
file, err := os.Open(filePath)
if err != nil {
return 0, err2.ENewFileError(err)
}
defer file.Close()
num := 0
inputReader := bufio.NewReader(file)
for {
_, readerError := inputReader.ReadString('\n')
if readerError == io.EOF {
num++
break
}
num++
}
return num, nil
}
func ReadFileLineNumExceptEmptyLine(filePath string) (int, *err2.FileError) {
file, err := os.Open(filePath)
if err != nil {
return 0, err2.ENewFileError(err)
}
defer file.Close()
num := 0
inputReader := bufio.NewReader(file)
for {
input, readerError := inputReader.ReadString('\n')
if readerError == io.EOF {
if input != "\r\n" && input != "" {
num++
}
break
}
if input != "\r\n" && input != "" {
num++
}
}
return num, nil
}
+155 -31
View File
@@ -172,30 +172,8 @@ func GetAllFileFromDir(filePath string) []*os.File {
return getFiles(filePath)
}
/*
* @param
* @return
* @description 删除文件夹下面所有的空文件夹
*/
func DeleteEmptyDir(filePath string) *err2.FileError {
err, is, _ := IsDirExist(filePath)
if err != nil {
return err2.ENewFileError(err)
}
if !is {
return err2.NewFileError("非文件夹")
}
//拿到所有的空文件夹
emptyDirList := getEmptyDir(filePath)
for _, row := range emptyDirList {
err := os.RemoveAll(row)
if err != nil {
return err2.ENewFileError(err)
}
}
return nil
func GetAllFileNameFromDir(filePath string) []string {
return getFileNames(filePath)
}
/*
@@ -211,25 +189,141 @@ func GetAllEmptyDir(dir string) (*err2.FileError, []string) {
if !is {
return err2.NewFileError("非文件夹"), nil
}
return nil, getEmptyDir(dir)
list := make([]string, 0)
errE, paths := GetAllDir(dir)
if errE != nil {
return errE, nil
}
for _, row := range paths {
err, is := isEmptyDir(row)
if err != nil {
return err, nil
}
if is {
list = append(list, row)
}
}
return nil, list
}
func getEmptyDir(dir string) []string {
emptyDirList := make([]string, 0)
/*
* 获取所有非空文件夹的路径集合
*/
func GetAllNotEmptyDir(dir string) (*err2.FileError, []string) {
err, is, _ := IsDirExist(dir)
if err != nil {
return err2.ENewFileError(err), nil
}
if !is {
return err2.NewFileError("非文件夹"), nil
}
files := make([]string, 0)
err1, aFiles := GetAllDir(dir)
if err1 != nil {
return err1, nil
}
err3, eFiles := GetAllEmptyDir(dir)
if err3 != nil {
return err3, nil
}
for _, a := range aFiles {
isIn := false
for _, e := range eFiles {
if a == e {
isIn = true
break
}
}
if !isIn {
files = append(files, a)
}
}
return nil, files
}
/*
* 获取目录下的所有文件夹路径集合
*/
func GetAllDir(dir string) (*err2.FileError, []string) {
err, is, _ := IsDirExist(dir)
if err != nil {
return err2.ENewFileError(err), nil
}
if !is {
return err2.NewFileError("非文件夹"), nil
}
return nil, getAllDir(dir)
}
/*
* @param
* @return
* @description 删除文件夹下面所有的空文件夹
*/
func DeleteEmptyDir(filePath string) *err2.FileError {
err, is, _ := IsDirExist(filePath)
if err != nil {
return err2.ENewFileError(err)
}
if !is {
return err2.NewFileError("非文件夹")
}
//拿到所有的空文件夹
err, emptyDirList := GetAllEmptyDir(filePath)
if err != nil {
return err
}
for _, row := range emptyDirList {
err := os.RemoveAll(row)
if err != nil {
return err2.ENewFileError(err)
}
}
return nil
}
func getAllDir(dir string) []string {
dirList := make([]string, 0)
info, err := ioutil.ReadDir(dir)
if err != nil {
return nil
}
for _, f := range info {
if f.IsDir() {
list := getFiles(fmt.Sprint(dir, "\\", f.Name()))
if list == nil || len(list) == 0 {
emptyDirList = append(emptyDirList, dir)
list := getAllDir(fmt.Sprint(dir, "\\", f.Name()))
dirList = append(dirList, fmt.Sprint(dir, "\\", f.Name()))
if list != nil {
for _, l := range list {
dirList = append(dirList, l)
}
}
}
}
return emptyDirList
return dirList
}
func isEmptyDir(dir string) (*err2.FileError, bool) {
_, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
file := GetAllFileFromDir(dir)
for _, f := range file {
info, err := f.Stat()
if err != nil {
return err2.ENewFileError(err), false
}
if !info.IsDir() {
return nil, false
}
}
return nil, true
}
func getFiles(filePath string) []*os.File {
@@ -240,6 +334,7 @@ func getFiles(filePath string) []*os.File {
}
for _, f := range info {
//处理文件夹的部分
if f.IsDir() {
list := getFiles(fmt.Sprint(filePath, "\\", f.Name()))
if list != nil {
@@ -247,8 +342,37 @@ func getFiles(filePath string) []*os.File {
fileList = append(fileList, row)
}
}
} else {
//处理文件的部分 -- 使用读写方法打开文件
thisFile, _ := os.OpenFile(fmt.Sprint(filePath, "\\", f.Name()), os.O_RDWR, 0666)
fileList = append(fileList, thisFile)
}
}
return fileList
}
func getFileNames(filePath string) []string {
nameList := make([]string, 0)
info, err := ioutil.ReadDir(filePath)
if err != nil {
return nil
}
for _, f := range info {
//处理文件夹的部分
if f.IsDir() {
list := getFileNames(fmt.Sprint(filePath, "\\", f.Name()))
if list != nil {
for _, row := range list {
nameList = append(nameList, row)
}
}
} else {
//处理文件的部分
nameList = append(nameList, fmt.Sprint(filePath, "\\", f.Name()))
}
}
return nameList
}
+120 -119
View File
@@ -6,122 +6,123 @@
package ini
import (
"gopkg.in/ini.v1"
"time"
)
func NewIniFileObj(iniPath string) (*ini.File, error) {
return ini.Load(iniPath)
}
/*
* @param f ini文件对象
* @return name->Section 的Map
* @description 通过所有Section对应的name->Section的map
*/
func GetSectionMap(f *ini.File) map[string]*ini.Section {
sectionMap := make(map[string]*ini.Section, 0)
sections := f.Sections()
for _, row := range sections {
sectionMap[row.Name()] = row
}
return sectionMap
}
/*
* @param f ini文件对象
* @param sectionName 区间名字
* @return key->value的map
* @description 返回对应Section名字的key-value的map结构,如果没有这个section返回空map
*/
func GetKeyValueBySection(f *ini.File, sectionName string) map[string]string {
keyValueMap := make(map[string]string, 0)
sec := f.Section(sectionName)
for _, row := range sec.Keys() {
keyValueMap[row.Name()] = row.Value()
}
return keyValueMap
}
/*
* @param f ini文件对象
* @return {section -> [{key->value}]}
* @description 返回每个区间对应的所有keyvalue的结构,具体返回结构如下:
* {
* "sectionName1":[
"keyName1":KeyValue1
* .....
* ],
* .....
* }
*/
func GetKeyValueByALLSection(f *ini.File) {
secMap := make(map[string][]map[string]string, 0)
for _, row := range f.Sections() {
keyList := make([]map[string]string, 0)
for _, r := range row.Keys() {
keyMap := make(map[string]string, 0)
keyMap[r.Name()] = r.Value()
keyList = append(keyList, keyMap)
}
secMap[row.Name()] = keyList
}
}
/*
* 使用例子,官网说明文档可以看到,这边补记录一下.
*/
func example(cfg *ini.File) {
//获取键值时设定候选值,如果没有找到,则返回默认值
cfg.Section("SectionName").Key("KeyName").In("default", []string{"str", "arr", "types"})
cfg.Section("SectionName").Key("KeyName").InFloat64(1.1, []float64{1.25, 2.5, 3.75})
cfg.Section("SectionName").Key("KeyName").InInt(5, []int{10, 20, 30})
cfg.Section("SectionName").Key("KeyName").InInt64(10, []int64{10, 20, 30})
cfg.Section("SectionName").Key("KeyName").InUint(4, []uint{3, 6, 9})
cfg.Section("SectionName").Key("KeyName").InUint64(8, []uint64{3, 6, 9})
//这边的三个时间不一样,为了防止报错这么写...
cfg.Section("SectionName").Key("KeyName").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time.Now(), time.Now(), time.Now()})
cfg.Section("SectionName").Key("KeyName").InTime(time.Now(), []time.Time{time.Now(), time.Now(), time.Now()}) // RFC3339
//验证获取的值是否在指定范围内,第一个参数是默认值,后面两个值分别是范围
cfg.Section("SectionName").Key("KeyName").RangeFloat64(0.0, 1.1, 2.2)
cfg.Section("SectionName").Key("KeyName").RangeInt(0, 10, 20)
cfg.Section("SectionName").Key("KeyName").RangeInt64(0, 10, 20)
cfg.Section("SectionName").Key("KeyName").RangeTimeFormat(time.RFC3339, time.Now(), time.Now(), time.Now())
cfg.Section("SectionName").Key("KeyName").RangeTime(time.Now(), time.Now(), time.Now()) // RFC3339
//自动分割键值到切片,当存在无效输入时,使用零值代替
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// Input: how, 2.2, are, you -> [0.0 2.2 0.0 0.0]
cfg.Section("SectionName").Key("KeyName").Strings(",")
cfg.Section("SectionName").Key("KeyName").Float64s(",")
cfg.Section("SectionName").Key("KeyName").Ints(",")
cfg.Section("SectionName").Key("KeyName").Int64s(",")
cfg.Section("SectionName").Key("KeyName").Uints(",")
cfg.Section("SectionName").Key("KeyName").Uint64s(",")
cfg.Section("SectionName").Key("KeyName").Times(",")
//自动分割键值到切片,从结果切片中剔除无效输入
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// Input: how, 2.2, are, you -> [2.2]
cfg.Section("SectionName").Key("KeyName").ValidFloat64s(",")
cfg.Section("SectionName").Key("KeyName").ValidInts(",")
cfg.Section("SectionName").Key("KeyName").ValidInt64s(",")
cfg.Section("SectionName").Key("KeyName").ValidUints(",")
cfg.Section("SectionName").Key("KeyName").ValidUint64s(",")
cfg.Section("SectionName").Key("KeyName").ValidTimes(",")
//自动分割键值到切片,当存在无效输入时,直接返回错误
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// Input: how, 2.2, are, you -> error
cfg.Section("SectionName").Key("KeyName").StrictFloat64s(",")
cfg.Section("SectionName").Key("KeyName").StrictInts(",")
cfg.Section("SectionName").Key("KeyName").StrictInt64s(",")
cfg.Section("SectionName").Key("KeyName").StrictUints(",")
cfg.Section("SectionName").Key("KeyName").StrictUint64s(",")
cfg.Section("SectionName").Key("KeyName").StrictTimes(",")
}
//
//import (
// "go-ini/ini"
// "time"
//)
//
//func NewIniFileObj(iniPath string) (*ini.File, error) {
// return ini.Load(iniPath)
//}
//
///*
// * @param f ini文件对象
// * @return name->Section 的Map
// * @description 通过所有Section对应的name->Section的map
// */
//func GetSectionMap(f *ini.File) map[string]*ini.Section {
// sectionMap := make(map[string]*ini.Section, 0)
// sections := f.Sections()
// for _, row := range sections {
// sectionMap[row.Name()] = row
// }
//
// return sectionMap
//}
//
///*
// * @param f ini文件对象
// * @param sectionName 区间名字
// * @return key->value的map
// * @description 返回对应Section名字的key-value的map结构,如果没有这个section返回空map
// */
//func GetKeyValueBySection(f *ini.File, sectionName string) map[string]string {
// keyValueMap := make(map[string]string, 0)
// sec := f.Section(sectionName)
// for _, row := range sec.Keys() {
// keyValueMap[row.Name()] = row.Value()
// }
//
// return keyValueMap
//}
//
///*
// * @param f ini文件对象
// * @return {section -> [{key->value}]}
// * @description 返回每个区间对应的所有key、value的结构,具体返回结构如下:
// * {
// * "sectionName1":[
// "keyName1":KeyValue1
// * .....
// * ],
// * .....
// * }
//*/
//func GetKeyValueByALLSection(f *ini.File) {
// secMap := make(map[string][]map[string]string, 0)
// for _, row := range f.Sections() {
// keyList := make([]map[string]string, 0)
// for _, r := range row.Keys() {
// keyMap := make(map[string]string, 0)
// keyMap[r.Name()] = r.Value()
// keyList = append(keyList, keyMap)
// }
//
// secMap[row.Name()] = keyList
// }
//}
//
///*
// * 使用例子,官网说明文档可以看到,这边补记录一下.
// */
//func example(cfg *ini.File) {
// //获取键值时设定候选值,如果没有找到,则返回默认值
// cfg.Section("SectionName").Key("KeyName").In("default", []string{"str", "arr", "types"})
// cfg.Section("SectionName").Key("KeyName").InFloat64(1.1, []float64{1.25, 2.5, 3.75})
// cfg.Section("SectionName").Key("KeyName").InInt(5, []int{10, 20, 30})
// cfg.Section("SectionName").Key("KeyName").InInt64(10, []int64{10, 20, 30})
// cfg.Section("SectionName").Key("KeyName").InUint(4, []uint{3, 6, 9})
// cfg.Section("SectionName").Key("KeyName").InUint64(8, []uint64{3, 6, 9})
// //这边的三个时间不一样,为了防止报错这么写...
// cfg.Section("SectionName").Key("KeyName").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time.Now(), time.Now(), time.Now()})
// cfg.Section("SectionName").Key("KeyName").InTime(time.Now(), []time.Time{time.Now(), time.Now(), time.Now()}) // RFC3339
//
// //验证获取的值是否在指定范围内,第一个参数是默认值,后面两个值分别是范围
// cfg.Section("SectionName").Key("KeyName").RangeFloat64(0.0, 1.1, 2.2)
// cfg.Section("SectionName").Key("KeyName").RangeInt(0, 10, 20)
// cfg.Section("SectionName").Key("KeyName").RangeInt64(0, 10, 20)
// cfg.Section("SectionName").Key("KeyName").RangeTimeFormat(time.RFC3339, time.Now(), time.Now(), time.Now())
// cfg.Section("SectionName").Key("KeyName").RangeTime(time.Now(), time.Now(), time.Now()) // RFC3339
//
// //自动分割键值到切片,当存在无效输入时,使用零值代替
// // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// // Input: how, 2.2, are, you -> [0.0 2.2 0.0 0.0]
// cfg.Section("SectionName").Key("KeyName").Strings(",")
// cfg.Section("SectionName").Key("KeyName").Float64s(",")
// cfg.Section("SectionName").Key("KeyName").Ints(",")
// cfg.Section("SectionName").Key("KeyName").Int64s(",")
// cfg.Section("SectionName").Key("KeyName").Uints(",")
// cfg.Section("SectionName").Key("KeyName").Uint64s(",")
// cfg.Section("SectionName").Key("KeyName").Times(",")
//
// //自动分割键值到切片,从结果切片中剔除无效输入
// // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// // Input: how, 2.2, are, you -> [2.2]
// cfg.Section("SectionName").Key("KeyName").ValidFloat64s(",")
// cfg.Section("SectionName").Key("KeyName").ValidInts(",")
// cfg.Section("SectionName").Key("KeyName").ValidInt64s(",")
// cfg.Section("SectionName").Key("KeyName").ValidUints(",")
// cfg.Section("SectionName").Key("KeyName").ValidUint64s(",")
// cfg.Section("SectionName").Key("KeyName").ValidTimes(",")
//
// //自动分割键值到切片,当存在无效输入时,直接返回错误
// // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
// // Input: how, 2.2, are, you -> error
// cfg.Section("SectionName").Key("KeyName").StrictFloat64s(",")
// cfg.Section("SectionName").Key("KeyName").StrictInts(",")
// cfg.Section("SectionName").Key("KeyName").StrictInt64s(",")
// cfg.Section("SectionName").Key("KeyName").StrictUints(",")
// cfg.Section("SectionName").Key("KeyName").StrictUint64s(",")
// cfg.Section("SectionName").Key("KeyName").StrictTimes(",")
//}