Skip to content

Commit

Permalink
Merge pull request #8 from wegoteam/dev
Browse files Browse the repository at this point in the history
v1.1.7
  • Loading branch information
wego-xuchang committed Aug 8, 2023
2 parents 8aabd73 + 4ddf4ff commit 47d07f1
Show file tree
Hide file tree
Showing 31 changed files with 2,208 additions and 33 deletions.
177 changes: 152 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
- 切片
- 日志
- 定时任务
...
- ...



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



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



### config
加载配置:默认加载环境变量、配置文件、命令行参数
- 默认配置文件加载顺序:命令行参数 > 默认配置文件目录(./config/config.yaml)
Expand Down Expand Up @@ -175,10 +177,10 @@ func TestTime(t *testing.T) {
```
### bean
-[x] 属性复制
-[x] 结构体转map
-[x] map转结构体
-[x] 结构体字段、tag、值获取
- [x] 属性复制
- [x] 结构体转map
- [x] map转结构体
- [x] 结构体字段、tag、值获取
```go
type A struct {
Age int `json:"age"`
Expand Down Expand Up @@ -366,6 +368,17 @@ func TestUUID(t *testing.T) {
}
```
#### random
随机字符串,随机数字
```go
func TestRandom(t *testing.T) {
randomStr := rand.RandomStr(10)
randomNum := rand.RandomNum(10)
fmt.Printf("randomStr: %s\n", randomStr)
fmt.Printf("randomNum: %s\n", randomNum)
}
```
### log
日志记录:支持日志文件切割,日志级别,日志格式化,日志文件压缩,日志文件清理
Expand Down Expand Up @@ -556,14 +569,13 @@ func TestPost(t *testing.T) {
```
### io
-[ ] 文件
-[x] json
-[x] xml
-[ ] csv
-[ ] excel
-[ ] doc
-[x] 压缩字符串
-[ ] 压缩文件
- [x] 文件
- [x] json
- [x] xml
- [x] excel
- [ ] doc/pdf
- [x] 压缩字符串
- [ ] 压缩文件
#### io/json
json序列化和反序列化
```go
Expand Down Expand Up @@ -608,14 +620,129 @@ func TestCompress(t *testing.T) {
}
```
贡献来源:
#### io/file
文件操作:复制、移动等
```go
func TestFile(t *testing.T) {
contentType, ext, parent := file.GetFileType("./testdata/a.txt")
fmt.Printf("contentType=%v, ext=%v, parent=%v \n", contentType, ext, parent)
fileType := file.GetFileExt("./testdata/a.txt")
fmt.Printf("fileType=%v \n", fileType)
isOn32bitArch := file.IsOn32bitArch()
fmt.Printf("是否32位系统架构:%v \n", isOn32bitArch)
isOn64bitArch := file.IsOn64bitArch()
fmt.Printf("是否64位系统架构:%v \n", isOn64bitArch)
isOnLinux := file.IsOnLinux()
fmt.Printf("是否Linux系统:%v \n", isOnLinux)
isOnMacOS := file.IsOnMacOS()
fmt.Printf("是否MacOS系统:%v \n", isOnMacOS)
isOnWindows := file.IsOnWindows()
fmt.Printf("是否Windows系统:%v \n", isOnWindows)
//file.ChangeExeDir()
existDir := file.ExistDir("./testdata")
fmt.Printf("是否存在目录:%v \n", existDir)
existFile := file.ExistFile("./testdata/a.txt")
fmt.Printf("是否存在文件:%v \n", existFile)
existSymlink := file.ExistSymlink("./testdata/a.txt")
fmt.Printf("是否存在软连接:%v \n", existSymlink)
isDirEmpty, _ := file.IsDirEmpty("./testdata")
fmt.Printf("目录是否为空:%v \n", isDirEmpty)
isFileEmpty, _ := file.IsFileEmpty("./testdata/a.txt")
fmt.Printf("文件是否为空:%v \n", isFileEmpty)
size, _ := file.GetDirSize("./testdata")
fmt.Printf("目录大小:%v \n", size)
fileSize, _ := file.GetFileSize("./testdata/a.txt")
fmt.Printf("文件大小:%v \n", fileSize)
symlinkSize, _ := file.GetSymlinkSize("./testdata/a.txt")
fmt.Printf("软连接大小:%v \n", symlinkSize)
sameDirEntries, _ := file.SameDirEntries("./testdata", "./testdata")
fmt.Printf("目录是否相同:%v \n", sameDirEntries)
sameFileContent, _ := file.SameFileContent("./testdata/a.txt", "./testdata/a.txt")
fmt.Printf("文件是否相同:%v \n", sameFileContent)
sameSymlinkContent, _ := file.SameSymlinkContent("./testdata/a.txt", "./testdata/a.txt")
fmt.Printf("软连接是否相同:%v \n", sameSymlinkContent)
listDir, _ := file.ListDir("./testdata")
fmt.Printf("目录列表:%v \n", listDir)
listFile, _ := file.ListFile("./testdata")
fmt.Printf("文件列表:%v \n", listFile)
listSymlink, _ := file.ListSymlink("./testdata")
fmt.Printf("软连接列表:%v \n", listSymlink)
err := file.CopyDir("./testdata", "./testdata2")
fmt.Printf("复制目录错误:%v \n", err)
err = file.CopyFile("./testdata/a.txt", "./testdata2/a.txt")
fmt.Printf("复制文件错误:%v \n", err)
err = file.CopySymlink("./testdata/a.txt", "./testdata2/a.txt")
fmt.Printf("复制软连接错误:%v \n", err)
err = file.MoveDir("./testdata", "./testdata2")
fmt.Printf("移动目录错误:%v \n", err)
err = file.MoveFile("./testdata/a.txt", "./testdata2/a.txt")
fmt.Printf("移动文件错误:%v \n", err)
err = file.MoveSymlink("./testdata/a.txt", "./testdata2/a.txt")
fmt.Printf("移动软连接错误:%v \n", err)
listMatch, _ := file.ListMatch("./testdata", file.ListIncludeAll, "*.txt")
fmt.Printf("匹配列表:%v \n", listMatch)
joinPath := file.JoinPath("./testdata", "a.txt")
fmt.Printf("拼接路径:%v \n", joinPath)
exist := file.Exist("./testdata/a.txt")
fmt.Printf("是否存在:%v \n", exist)
notExist := file.NotExist("./testdata/a.txt")
fmt.Printf("是否不存在:%v \n", notExist)
err = file.MakeDir("./testdata2")
fmt.Printf("创建目录错误:%v \n", err)
//根据名称排序
sort.Stable(file.SortListByName(listFile))
//根据大小排序
sort.Stable(file.SortListBySize(listFile))
//根据修改时间排序
sort.Stable(file.SortListByModTime(listFile))
}
```
#### io/excel
excel操作
```go
https://github.com/spf13/viper
```
https://github.com/redis/go-redis
### job
任务调度
#### jon/cron
定时任务
```text
cron表达式语法:
┌──分钟(0 - 59)
│ ┌──小时(0 - 23)
│ │ ┌──日(1 - 31)
│ │ │ ┌─月(1 - 12)
│ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
│ │ │ │ │
* * * * * 被执行的命令
秒 分 时 日 月 周
```
使用方法
```go
func TestCron(t *testing.T) {
jonID, _ := cron.AddJob("0/2 * * * * ?", JobTest1)
cron.AddJob("0/2 * * * * ?", JobTest2)
time.Sleep(time.Second * 10)
cron.DelJob(jonID)
time.Sleep(time.Second * 10)
}
https://github.com/jeevatkm/go-model
func JobTest1() {
fmt.Println("JobTest1")
}
https://github.com/go-resty/resty
func JobTest2() {
fmt.Println("JobTest2")
}
```
https://github.com/golang-module/carbon
## 引用列表
- https://github.com/spf13/viper
- https://github.com/redis/go-redis
- https://github.com/jeevatkm/go-model
- https://github.com/go-resty/resty
- https://github.com/golang-module/carbon
- https://github.com/qax-os/excelize
28 changes: 28 additions & 0 deletions example/cron_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package example

import (
"fmt"
"github.com/wegoteam/wepkg/job/cron"
"testing"
"time"
)

func TestCron(t *testing.T) {
job1, _ := cron.AddJob("0/1 * * * * ?", JobTest1)
job2, _ := cron.AddJob("0/1 * * * * ?", JobTest2)
time.Sleep(time.Second * 3)
cron.DelJob(job1)
cron.DelJob(job2)
job3, _ := cron.AddJob("0/1 * * * * ?", JobTest1)
time.Sleep(time.Second * 3)
cron.DelJob(job3)
time.Sleep(time.Second * 3)
}

func JobTest1() {
fmt.Println("JobTest1")
}

func JobTest2() {
fmt.Println("JobTest2")
}
98 changes: 98 additions & 0 deletions example/excel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package example

import (
"fmt"
"github.com/wegoteam/wepkg/io/excel"
"github.com/xuri/excelize/v2"
"testing"
)

func TestCreateExcelFile(t *testing.T) {
//https://github.com/qax-os/excelize
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Create a new sheet.
//_, err := f.NewSheet("Sheet2")
//if err != nil {
// fmt.Println(err)
// return
//}
// Set value of a cell.
//f.SetCellValue("Sheet2", "A2", "Hello world.")
//f.SetCellValue("Sheet1", "B2", 100)

for idx, row := range [][]interface{}{
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
} {
cell, err := excelize.CoordinatesToCellName(1, idx+1)
if err != nil {
fmt.Println(err)
return
}
f.SetSheetRow("Sheet1", cell, &row)
}
// Set active sheet of the workbook.
f.SetActiveSheet(0)
// Save spreadsheet by the given path.
if err := f.SaveAs("./testdata/Book1.xlsx"); err != nil {
fmt.Println(err)
}

}

func TestReadExcel(t *testing.T) {
f, err := excelize.OpenFile("./testdata/Book1.xlsx")

if err != nil {
fmt.Println(err)
return
}
defer func() {
// Close the spreadsheet.
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Get value from cell by given worksheet name and cell reference.
cell, err := f.GetCellValue("Sheet1", "B2")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(cell)
// Get all the rows in the Sheet1.
rows, err := f.GetRows("Sheet1")
if err != nil {
fmt.Println(err)
return
}
for _, row := range rows {
for _, colCell := range row {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
}

func TestExcelFile(t *testing.T) {
var datas = make([][]interface{}, 0)
datas = append(datas, []interface{}{"姓名", "年龄", "性别"})
datas = append(datas, []interface{}{"张三", 18, "男"})
datas = append(datas, []interface{}{"李四", 20, "女"})
datas = append(datas, []interface{}{"王五", 22, "男"})
err := excel.SaveExcelPath("./testdata/Book2.xlsx", datas)
if err != nil {
fmt.Println(err)
}

err = excel.SaveExcelWriter(nil, datas)
if err != nil {
fmt.Println(err)
}

}
Loading

0 comments on commit 47d07f1

Please sign in to comment.