奇技指南
今天给大家介绍下我们自主开发的go语言轻型框架gobox,分享它的由来以及gobox中的异常定义和杂项工具。
gobox简介
gobox我们自主开发的go语言轻型框架
目前它的项目地址为:
https://github.com/goinbox
前世今生
我最开始用go写的第一个项目是一个代码发布系统,里面参考Raft实现了一套代码发布机故障自动检测恢复的机制。
那个时候刚刚接触go语言,开发时用到的一些库都是仿照之前用别的语言开发时的库用go重新实现的,随着我对go越来越熟悉,项目中用的也越来越多,我们重构了这个库,并命名为gobox。
随着go官方推出了dep这个包管理工具,我们把gobox中的每一个box都单独拿出来作为一个项目管理,这就是现在的gobox:
https://github.com/goinbox。
命名的由来
为什么叫gobox呢?
因为我们设计让每一个单独的模块都作为一个box,那这些box的集合就称为gobox,再使用go的pkg管理机制引入到项目中。
目前支持的box
├── color // 为终端输出增加颜色├── crypto // 常用加解密相关├── encoding // 常用编解码相关├── exception // 用errno+msg表示的错误├── gohttp // http服务相关├── golog // log记录相关├── gomisc // 零碎的工具库├── go-nsq-mate // 配合操作nsq队列├── inotify // 文件系统通知├── levelcache // 使用leveldb实现的本地cache├── mysql // mysql操作相关├── page // 分页操作├── pool // 连接池实现├── redis // redis操作相关├── shardmap // 为减小map锁粒度实现的碎片化map├── shell // 执行shell命令相关├── simplecache // 内存cache
使用方式
每个box都相当于一个pkg,使用也很简单,示例:
package mainimport ( "github.com/goinbox/color" "fmt")func main() { fmt.Println(string(color.Black([]byte("Black")))) fmt.Println(string(color.Red([]byte("Red")))) fmt.Println(string(color.Green([]byte("Green")))) fmt.Println(string(color.Yellow([]byte("Yellow")))) fmt.Println(string(color.Blue([]byte("Blue")))) fmt.Println(string(color.Maganta([]byte("Maganta")))) fmt.Println(string(color.Cyan([]byte("Cyan")))) fmt.Println(string(color.White([]byte("White"))))}
go官方提供了dep工具,配合在项目中使用可以更加方便。
dep: https://golang.github.io/dep/
exception
很多语言提供了异常机制,但是go没有,相似的能力可以用panic/recover来模拟,但是官方并不推荐这样做。
我们在系统中定义错误时通常需要错误码errno和错误信息msg,这个包就是简单的包装了下这两个常用的错误内容。
用法示例
package mainimport ( "github.com/goinbox/exception" "fmt")func main() { e := exception.New(101, "test exception") fmt.Println(e.Errno(), e.Msg()) fmt.Println(e.Error())}
输出效果示例
101 test exceptionerrno: 101, msg: test exception
gomisc
gomisc提供了很多工具方法
slice去重
这里仅对最常用的int[]和string[]提供了方法,示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { is := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4} fmt.Println("origin slice is: ", is) is = gomisc.IntSliceUnique(is) fmt.Println("after call slice is: ", is) ss := []string{"a", "ab", "ab", "abc", "abc", "abc", "abcd", "abcd", "abcd", "abcd", "abcd"} fmt.Println("origin slice is: ", ss) ss = gomisc.StringSliceUnique(ss) fmt.Println("after call slice is: ", ss)}
结果输出:
origin slice is: [1 2 2 3 3 3 4 4 4 4]after call slice is: [1 2 3 4]origin slice is: [a ab ab abc abc abc abcd abcd abcd abcd abcd]after call slice is: [a ab abc abcd]
检查文件或是目录是否存在
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { f := "/etc/passwd" r := gomisc.FileExist(f) if r { fmt.Println(f, "is exist") } else { fmt.Println(f, "is not exist") } d := "/home/ligang/devspace" r = gomisc.DirExist(d) if r { fmt.Println(d, "is exist") } else { fmt.Println(d, "is not exist") }}
结果输出:
/etc/passwd is exist/home/ligang/devspace is exist
[]byte追加
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { b := []byte("abc") b = gomisc.AppendBytes(b, []byte("def"), []byte("ghi")) fmt.Println(string(b))}
结果输出:
abcdefghi
递归获取指定根目录下的所有文件,包括子目录
这里的实现,解决了目录过深时栈溢出的问题,示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { fileList, err := gomisc.ListFilesInDir("/home/ligang/tmp") if err != nil { fmt.Println(err) return } for _, path := range fileList { fmt.Println(path) }}
输出:
root dir is: /home/ligang/tmpfile list under root dir are:/home/ligang/tmp/misc/go1.10.2.linux-amd64.tar.gz/home/ligang/tmp/misc/CLion-2018.1.2.tar.gz/home/ligang/tmp/misc/docker-18.03.1-ce.tgz/home/ligang/tmp/go/main.go/home/ligang/tmp/go/.idea/modules.xml/home/ligang/tmp/go/.idea/workspace.xml/home/ligang/tmp/go/.idea/go.iml
保存和解析json文件
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { filePath := "/tmp/test_save_parse_json_file.json" v1 := make(map[string]string) v1["k1"] = "a" v1["k2"] = "b" v1["k3"] = "c" err := gomisc.SaveJsonFile(filePath, v1) if err != nil { fmt.Println("save json file failed: " + err.Error()) } else { fmt.Println("save json file success") } v2 := make(map[string]string) err = gomisc.ParseJsonFile(filePath, &v2) if err != nil { fmt.Println("parse json file failed: " + err.Error()) } else { fmt.Println("parse json file success") } for k, v := range v2 { if v != v1[k] { fmt.Println("save parse json file error, k: " + k + " not equal") } else { fmt.Println("save parse json file k:", k, "equal") } }}
输出:
字符串截取
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt")func main() { s := "abcdefg" _, err := gomisc.SubString(s, 3, 20) fmt.Println(err) _, err = gomisc.SubString(s, 10, 3) fmt.Println(err) ss, _ := gomisc.SubString(s, 3, 4) fmt.Println(s, "substr", "3,4", "is", ss)}
输出:
end too longend too longabcdefg substr 3,4 is defg
时间格式化时的常量定义
TIME_FMT_STR_YEAR = "2006"TIME_FMT_STR_MONTH = "01"TIME_FMT_STR_DAY = "02"TIME_FMT_STR_HOUR = "15"TIME_FMT_STR_MINUTE = "04"TIME_FMT_STR_SECOND = "05"
常用时间格式化时的格式
输出格式:yyyy-mm-dd h:i:s
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt" "time")func main() { layout := gomisc.TimeGeneralLayout() fmt.Println("fmt layout is", layout) fmt.Println("not time is", time.Now().Format(layout))}
输出:
fmt layout is 2006-01-02 15:04:05not time is 2018-05-20 06:15:08
通过时间生成随机数
示例:
package mainimport ( "github.com/goinbox/gomisc" "fmt" "time")func main() { tm := time.Now() fmt.Println(gomisc.RandByTime(&tm), gomisc.RandByTime(&tm), gomisc.RandByTime(nil))}
输出:
4423491624236117727 4423491624236117727 1471010178475409526
这里请注意:时间值相同时运算结果是相同的。
欢迎大家使用gobox,使用中有遇到问题,可在公众号后台反馈,我们会尽快响应,谢谢~~
界世的你当不
只做你的肩膀
无
360官方技术公众号
技术干货|一手资讯|精彩活动
空·