Skip to content

Commit 47d07f1

Browse files
authored
Merge pull request #8 from wegoteam/dev
v1.1.7
2 parents 8aabd73 + 4ddf4ff commit 47d07f1

31 files changed

+2208
-33
lines changed

README.md

Lines changed: 152 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
- 切片
1414
- 日志
1515
- 定时任务
16-
...
16+
- ...
1717

1818

1919

2020
## 更新记录
21-
- v1.0.5:json、xml、log
22-
- v1.0.4:datatime、http、config、crypto、uuid/ulid
23-
- v1.0.3:加载配置
24-
- v1.0.1:雪花算法
25-
- v1.0.0:bean属性拷贝
21+
- v1.1.7:io/file、io/excel
22+
- v1.0.6:id/random、job/cron
23+
- v1.0.5:io/json、io/xml、log
24+
- v1.0.4:datatime、http、config、crypto、id/uuid、id/ulid
25+
- v1.0.3:config
26+
- v1.0.2:id/snowflake
27+
- v1.0.1:id/snowflake
28+
- v1.0.0:bean
2629

2730

2831

@@ -39,7 +42,6 @@ go get -u github.com/wegoteam/wepkg@latest
3942
- 分页
4043

4144

42-
4345
### config
4446
加载配置:默认加载环境变量、配置文件、命令行参数
4547
- 默认配置文件加载顺序:命令行参数 > 默认配置文件目录(./config/config.yaml)
@@ -175,10 +177,10 @@ func TestTime(t *testing.T) {
175177
```
176178
177179
### bean
178-
-[x] 属性复制
179-
-[x] 结构体转map
180-
-[x] map转结构体
181-
-[x] 结构体字段、tag、值获取
180+
- [x] 属性复制
181+
- [x] 结构体转map
182+
- [x] map转结构体
183+
- [x] 结构体字段、tag、值获取
182184
```go
183185
type A struct {
184186
Age int `json:"age"`
@@ -366,6 +368,17 @@ func TestUUID(t *testing.T) {
366368
}
367369
```
368370
371+
#### random
372+
随机字符串,随机数字
373+
```go
374+
func TestRandom(t *testing.T) {
375+
randomStr := rand.RandomStr(10)
376+
randomNum := rand.RandomNum(10)
377+
fmt.Printf("randomStr: %s\n", randomStr)
378+
fmt.Printf("randomNum: %s\n", randomNum)
379+
}
380+
```
381+
369382
### log
370383
日志记录:支持日志文件切割,日志级别,日志格式化,日志文件压缩,日志文件清理
371384
@@ -556,14 +569,13 @@ func TestPost(t *testing.T) {
556569
```
557570
558571
### io
559-
-[ ] 文件
560-
-[x] json
561-
-[x] xml
562-
-[ ] csv
563-
-[ ] excel
564-
-[ ] doc
565-
-[x] 压缩字符串
566-
-[ ] 压缩文件
572+
- [x] 文件
573+
- [x] json
574+
- [x] xml
575+
- [x] excel
576+
- [ ] doc/pdf
577+
- [x] 压缩字符串
578+
- [ ] 压缩文件
567579
#### io/json
568580
json序列化和反序列化
569581
```go
@@ -608,14 +620,129 @@ func TestCompress(t *testing.T) {
608620
}
609621
```
610622
611-
贡献来源:
623+
#### io/file
624+
文件操作:复制、移动等
625+
```go
626+
func TestFile(t *testing.T) {
627+
contentType, ext, parent := file.GetFileType("./testdata/a.txt")
628+
fmt.Printf("contentType=%v, ext=%v, parent=%v \n", contentType, ext, parent)
629+
fileType := file.GetFileExt("./testdata/a.txt")
630+
fmt.Printf("fileType=%v \n", fileType)
631+
isOn32bitArch := file.IsOn32bitArch()
632+
fmt.Printf("是否32位系统架构:%v \n", isOn32bitArch)
633+
isOn64bitArch := file.IsOn64bitArch()
634+
fmt.Printf("是否64位系统架构:%v \n", isOn64bitArch)
635+
isOnLinux := file.IsOnLinux()
636+
fmt.Printf("是否Linux系统:%v \n", isOnLinux)
637+
isOnMacOS := file.IsOnMacOS()
638+
fmt.Printf("是否MacOS系统:%v \n", isOnMacOS)
639+
isOnWindows := file.IsOnWindows()
640+
fmt.Printf("是否Windows系统:%v \n", isOnWindows)
641+
//file.ChangeExeDir()
642+
existDir := file.ExistDir("./testdata")
643+
fmt.Printf("是否存在目录:%v \n", existDir)
644+
existFile := file.ExistFile("./testdata/a.txt")
645+
fmt.Printf("是否存在文件:%v \n", existFile)
646+
existSymlink := file.ExistSymlink("./testdata/a.txt")
647+
fmt.Printf("是否存在软连接:%v \n", existSymlink)
648+
isDirEmpty, _ := file.IsDirEmpty("./testdata")
649+
fmt.Printf("目录是否为空:%v \n", isDirEmpty)
650+
isFileEmpty, _ := file.IsFileEmpty("./testdata/a.txt")
651+
fmt.Printf("文件是否为空:%v \n", isFileEmpty)
652+
size, _ := file.GetDirSize("./testdata")
653+
fmt.Printf("目录大小:%v \n", size)
654+
fileSize, _ := file.GetFileSize("./testdata/a.txt")
655+
fmt.Printf("文件大小:%v \n", fileSize)
656+
symlinkSize, _ := file.GetSymlinkSize("./testdata/a.txt")
657+
fmt.Printf("软连接大小:%v \n", symlinkSize)
658+
sameDirEntries, _ := file.SameDirEntries("./testdata", "./testdata")
659+
fmt.Printf("目录是否相同:%v \n", sameDirEntries)
660+
sameFileContent, _ := file.SameFileContent("./testdata/a.txt", "./testdata/a.txt")
661+
fmt.Printf("文件是否相同:%v \n", sameFileContent)
662+
sameSymlinkContent, _ := file.SameSymlinkContent("./testdata/a.txt", "./testdata/a.txt")
663+
fmt.Printf("软连接是否相同:%v \n", sameSymlinkContent)
664+
listDir, _ := file.ListDir("./testdata")
665+
fmt.Printf("目录列表:%v \n", listDir)
666+
listFile, _ := file.ListFile("./testdata")
667+
fmt.Printf("文件列表:%v \n", listFile)
668+
listSymlink, _ := file.ListSymlink("./testdata")
669+
fmt.Printf("软连接列表:%v \n", listSymlink)
670+
err := file.CopyDir("./testdata", "./testdata2")
671+
fmt.Printf("复制目录错误:%v \n", err)
672+
err = file.CopyFile("./testdata/a.txt", "./testdata2/a.txt")
673+
fmt.Printf("复制文件错误:%v \n", err)
674+
err = file.CopySymlink("./testdata/a.txt", "./testdata2/a.txt")
675+
fmt.Printf("复制软连接错误:%v \n", err)
676+
err = file.MoveDir("./testdata", "./testdata2")
677+
fmt.Printf("移动目录错误:%v \n", err)
678+
err = file.MoveFile("./testdata/a.txt", "./testdata2/a.txt")
679+
fmt.Printf("移动文件错误:%v \n", err)
680+
err = file.MoveSymlink("./testdata/a.txt", "./testdata2/a.txt")
681+
fmt.Printf("移动软连接错误:%v \n", err)
682+
listMatch, _ := file.ListMatch("./testdata", file.ListIncludeAll, "*.txt")
683+
fmt.Printf("匹配列表:%v \n", listMatch)
684+
joinPath := file.JoinPath("./testdata", "a.txt")
685+
fmt.Printf("拼接路径:%v \n", joinPath)
686+
exist := file.Exist("./testdata/a.txt")
687+
fmt.Printf("是否存在:%v \n", exist)
688+
notExist := file.NotExist("./testdata/a.txt")
689+
fmt.Printf("是否不存在:%v \n", notExist)
690+
err = file.MakeDir("./testdata2")
691+
fmt.Printf("创建目录错误:%v \n", err)
692+
//根据名称排序
693+
sort.Stable(file.SortListByName(listFile))
694+
//根据大小排序
695+
sort.Stable(file.SortListBySize(listFile))
696+
//根据修改时间排序
697+
sort.Stable(file.SortListByModTime(listFile))
698+
}
699+
```
700+
701+
#### io/excel
702+
excel操作
703+
```go
612704
613-
https://github.com/spf13/viper
705+
```
614706
615-
https://github.com/redis/go-redis
707+
### job
708+
任务调度
709+
#### jon/cron
710+
定时任务
711+
712+
```text
713+
cron表达式语法:
714+
┌──分钟(0 - 59)
715+
│ ┌──小时(0 - 23)
716+
│ │ ┌──日(1 - 31)
717+
│ │ │ ┌─月(1 - 12)
718+
│ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
719+
│ │ │ │ │
720+
* * * * * 被执行的命令
721+
秒 分 时 日 月 周
722+
```
723+
使用方法
724+
```go
725+
func TestCron(t *testing.T) {
726+
jonID, _ := cron.AddJob("0/2 * * * * ?", JobTest1)
727+
cron.AddJob("0/2 * * * * ?", JobTest2)
728+
time.Sleep(time.Second * 10)
729+
cron.DelJob(jonID)
730+
time.Sleep(time.Second * 10)
731+
}
616732
617-
https://github.com/jeevatkm/go-model
733+
func JobTest1() {
734+
fmt.Println("JobTest1")
735+
}
618736
619-
https://github.com/go-resty/resty
737+
func JobTest2() {
738+
fmt.Println("JobTest2")
739+
}
740+
```
620741
621-
https://github.com/golang-module/carbon
742+
## 引用列表
743+
- https://github.com/spf13/viper
744+
- https://github.com/redis/go-redis
745+
- https://github.com/jeevatkm/go-model
746+
- https://github.com/go-resty/resty
747+
- https://github.com/golang-module/carbon
748+
- https://github.com/qax-os/excelize

example/cron_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package example
2+
3+
import (
4+
"fmt"
5+
"github.com/wegoteam/wepkg/job/cron"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestCron(t *testing.T) {
11+
job1, _ := cron.AddJob("0/1 * * * * ?", JobTest1)
12+
job2, _ := cron.AddJob("0/1 * * * * ?", JobTest2)
13+
time.Sleep(time.Second * 3)
14+
cron.DelJob(job1)
15+
cron.DelJob(job2)
16+
job3, _ := cron.AddJob("0/1 * * * * ?", JobTest1)
17+
time.Sleep(time.Second * 3)
18+
cron.DelJob(job3)
19+
time.Sleep(time.Second * 3)
20+
}
21+
22+
func JobTest1() {
23+
fmt.Println("JobTest1")
24+
}
25+
26+
func JobTest2() {
27+
fmt.Println("JobTest2")
28+
}

example/excel_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package example
2+
3+
import (
4+
"fmt"
5+
"github.com/wegoteam/wepkg/io/excel"
6+
"github.com/xuri/excelize/v2"
7+
"testing"
8+
)
9+
10+
func TestCreateExcelFile(t *testing.T) {
11+
//https://github.com/qax-os/excelize
12+
f := excelize.NewFile()
13+
defer func() {
14+
if err := f.Close(); err != nil {
15+
fmt.Println(err)
16+
}
17+
}()
18+
// Create a new sheet.
19+
//_, err := f.NewSheet("Sheet2")
20+
//if err != nil {
21+
// fmt.Println(err)
22+
// return
23+
//}
24+
// Set value of a cell.
25+
//f.SetCellValue("Sheet2", "A2", "Hello world.")
26+
//f.SetCellValue("Sheet1", "B2", 100)
27+
28+
for idx, row := range [][]interface{}{
29+
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
30+
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
31+
} {
32+
cell, err := excelize.CoordinatesToCellName(1, idx+1)
33+
if err != nil {
34+
fmt.Println(err)
35+
return
36+
}
37+
f.SetSheetRow("Sheet1", cell, &row)
38+
}
39+
// Set active sheet of the workbook.
40+
f.SetActiveSheet(0)
41+
// Save spreadsheet by the given path.
42+
if err := f.SaveAs("./testdata/Book1.xlsx"); err != nil {
43+
fmt.Println(err)
44+
}
45+
46+
}
47+
48+
func TestReadExcel(t *testing.T) {
49+
f, err := excelize.OpenFile("./testdata/Book1.xlsx")
50+
51+
if err != nil {
52+
fmt.Println(err)
53+
return
54+
}
55+
defer func() {
56+
// Close the spreadsheet.
57+
if err := f.Close(); err != nil {
58+
fmt.Println(err)
59+
}
60+
}()
61+
// Get value from cell by given worksheet name and cell reference.
62+
cell, err := f.GetCellValue("Sheet1", "B2")
63+
if err != nil {
64+
fmt.Println(err)
65+
return
66+
}
67+
fmt.Println(cell)
68+
// Get all the rows in the Sheet1.
69+
rows, err := f.GetRows("Sheet1")
70+
if err != nil {
71+
fmt.Println(err)
72+
return
73+
}
74+
for _, row := range rows {
75+
for _, colCell := range row {
76+
fmt.Print(colCell, "\t")
77+
}
78+
fmt.Println()
79+
}
80+
}
81+
82+
func TestExcelFile(t *testing.T) {
83+
var datas = make([][]interface{}, 0)
84+
datas = append(datas, []interface{}{"姓名", "年龄", "性别"})
85+
datas = append(datas, []interface{}{"张三", 18, "男"})
86+
datas = append(datas, []interface{}{"李四", 20, "女"})
87+
datas = append(datas, []interface{}{"王五", 22, "男"})
88+
err := excel.SaveExcelPath("./testdata/Book2.xlsx", datas)
89+
if err != nil {
90+
fmt.Println(err)
91+
}
92+
93+
err = excel.SaveExcelWriter(nil, datas)
94+
if err != nil {
95+
fmt.Println(err)
96+
}
97+
98+
}

0 commit comments

Comments
 (0)