今天给大家介绍下我们自主开发的go语言轻型框架gobox,今天这一期,主要说下gobox中的异常定义和杂项工具。为什么叫gobox呢?因为我们设计让每一个单独的模块都作为一个box,那这些box的集合就称为gobox,再使用go的pkg管理机制引入到项目中。

 

exception

很多语言提供了异常机制,但是go没有,相似的能力可以用 panic/recover来模拟,但是官方并不推荐这样做。

我们在系统中定义错误时通常需要错误码errno和错误信息msg,这个包就是简单的包装了下这两个常用的错误内容。

用法示例

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/exception"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. e := exception.New(101, "test exception")

  11.  

  12. fmt.Println(e.Errno(), e.Msg())

  13. fmt.Println(e.Error())

  14. }

输出效果示例

  1. 101 test exception

  2. errno: 101, msg: test exception

gomisc

gomisc提供了很多工具方法

slice去重

这里仅对最常用的int[]和string[]提供了方法,示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. is := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}

  11. fmt.Println("origin slice is: ", is)

  12.  

  13. is = gomisc.IntSliceUnique(is)

  14. fmt.Println("after call slice is: ", is)

  15.  

  16. ss := []string{"a", "ab", "ab", "abc", "abc", "abc", "abcd", "abcd", "abcd", "abcd", "abcd"}

  17. fmt.Println("origin slice is: ", ss)

  18.  

  19. ss = gomisc.StringSliceUnique(ss)

  20. fmt.Println("after call slice is: ", ss)

  21. }

结果输出:

  1. origin slice is: [1 2 2 3 3 3 4 4 4 4]

  2. after call slice is: [1 2 3 4]

  3. origin slice is: [a ab ab abc abc abc abcd abcd abcd abcd abcd]

  4. after call slice is: [a ab abc abcd]

检查文件或是目录是否存在

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. f := "/etc/passwd"

  11.  

  12. r := gomisc.FileExist(f)

  13. if r {

  14. fmt.Println(f, "is exist")

  15. } else {

  16. fmt.Println(f, "is not exist")

  17. }

  18.  

  19. d := "/home/ligang/devspace"

  20.  

  21. r = gomisc.DirExist(d)

  22. if r {

  23. fmt.Println(d, "is exist")

  24. } else {

  25. fmt.Println(d, "is not exist")

  26. }

  27. }

结果输出:

  1. /etc/passwd is exist

  2. /home/ligang/devspace is exist

[]byte追加

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. b := []byte("abc")

  11. b = gomisc.AppendBytes(b, []byte("def"), []byte("ghi"))

  12.  

  13. fmt.Println(string(b))

  14. }

结果输出:

  1. abcdefghi

递归获取指定根目录下的所有文件,包括子目录

这里的实现,解决了目录过深时栈溢出的问题,示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. fileList, err := gomisc.ListFilesInDir("/home/ligang/tmp")

  11. if err != nil {

  12. fmt.Println(err)

  13. return

  14. }

  15.  

  16. for _, path := range fileList {

  17. fmt.Println(path)

  18. }

  19. }

输出:

  1. root dir is: /home/ligang/tmp

  2. file list under root dir are:

  3. /home/ligang/tmp/misc/go1.10.2.linux-amd64.tar.gz

  4. /home/ligang/tmp/misc/CLion-2018.1.2.tar.gz

  5. /home/ligang/tmp/misc/docker-18.03.1-ce.tgz

  6. /home/ligang/tmp/go/main.go

  7. /home/ligang/tmp/go/.idea/modules.xml

  8. /home/ligang/tmp/go/.idea/workspace.xml

  9. /home/ligang/tmp/go/.idea/go.iml

保存和解析json文件

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. filePath := "/tmp/test_save_parse_json_file.json"

  11.  

  12. v1 := make(map[string]string)

  13. v1["k1"] = "a"

  14. v1["k2"] = "b"

  15. v1["k3"] = "c"

  16.  

  17. err := gomisc.SaveJsonFile(filePath, v1)

  18. if err != nil {

  19. fmt.Println("save json file failed: " + err.Error())

  20. } else {

  21. fmt.Println("save json file success")

  22. }

  23.  

  24. v2 := make(map[string]string)

  25. err = gomisc.ParseJsonFile(filePath, &v2)

  26. if err != nil {

  27. fmt.Println("parse json file failed: " + err.Error())

  28. } else {

  29. fmt.Println("parse json file success")

  30. }

  31.  

  32. for k, v := range v2 {

  33. if v != v1[k] {

  34. fmt.Println("save parse json file error, k: " + k + " not equal")

  35. } else {

  36. fmt.Println("save parse json file k:", k, "equal")

  37. }

  38. }

  39. }

输出:

  1. save json file success

  2. parse json file success

  3. save parse json file k: k2 equal

  4. save parse json file k: k3 equal

  5. save parse json file k: k1 equal

字符串截取

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. )

  8.  

  9. func main() {

  10. s := "abcdefg"

  11.  

  12. _, err := gomisc.SubString(s, 3, 20)

  13. fmt.Println(err)

  14.  

  15. _, err = gomisc.SubString(s, 10, 3)

  16. fmt.Println(err)

  17.  

  18. ss, _ := gomisc.SubString(s, 3, 4)

  19. fmt.Println(s, "substr", "3,4", "is", ss)

  20. }

输出:

  1. end too long

  2. end too long

  3. abcdefg substr 3,4 is defg

时间格式化时的常量定义

  1. TIME_FMT_STR_YEAR = "2006"

  2. TIME_FMT_STR_MONTH = "01"

  3. TIME_FMT_STR_DAY = "02"

  4. TIME_FMT_STR_HOUR = "15"

  5. TIME_FMT_STR_MINUTE = "04"

  6. TIME_FMT_STR_SECOND = "05"

常用时间格式化时的格式

输出格式: yyyy-mm-dd h:i:s

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. "time"

  8. )

  9.  

  10. func main() {

  11. layout := gomisc.TimeGeneralLayout()

  12. fmt.Println("fmt layout is", layout)

  13.  

  14. fmt.Println("not time is", time.Now().Format(layout))

  15. }

输出:

  1. fmt layout is 2006-01-02 15:04:05

  2. not time is 2018-05-20 06:15:08

通过时间生成随机数

示例:

  1. package main

  2.  

  3. import (

  4. "github.com/goinbox/gomisc"

  5.  

  6. "fmt"

  7. "time"

  8. )

  9.  

  10. func main() {

  11. tm := time.Now()

  12. fmt.Println(gomisc.RandByTime(&tm), gomisc.RandByTime(&tm), gomisc.RandByTime(nil))

  13. }

输出:

  1. 4423491624236117727 4423491624236117727 1471010178475409526

这里请注意:时间值相同时运算结果是相同的。

欢迎大家使用,使用中有遇到问题随时反馈,我们会尽快响应,谢谢!

 

gobox中的异常定义和杂项工具_gobox