12

gobox中的常用工具包gomisc

 4 years ago
source link: http://blog.7rule.com/2018/09/08/gobox-misc.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

gobox中的常用工具包gomisc

Sep 8, 2018

有一些常用的工具函数,我们把它们放到gomisc这个包中。

Slice中的值Unique

func IntSliceUnique(s []int) []int
func StringSliceUnique(s []string) []string
package main

import (
	"github.com/goinbox/gomisc"

	"fmt"
)

func main() {
	TestIntSliceUnique()
	TestStringSliceUnique()
}

func TestIntSliceUnique() {
	s := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}

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

	s = gomisc.IntSliceUnique(s)

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

func TestStringSliceUnique() {
	s := []string{"a", "ab", "ab", "abc", "abc", "abc", "abcd", "abcd", "abcd", "abcd", "abcd"}

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

	s = gomisc.StringSliceUnique(s)

	fmt.Println("after call slice is: ", s)
}
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]

文件、目录是否存在

func FileExist(path string) bool
func DirExist(path string) bool
package main

import (
	"github.com/goinbox/gomisc"

	"fmt"
)

func main() {
	TestFileExist()
	TestDirExist()
}

func TestFileExist() {
	f := "/etc/passwd"

	r := gomisc.FileExist(f)
	if r {
		fmt.Println(f, "is exist")
	} else {
		fmt.Println(f, "is not exist")
	}
}

func TestDirExist() {
	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拼接

func AppendBytes(b []byte, elems ...[]byte) []byte
package main

import (
	"github.com/goinbox/gomisc"

	"fmt"
)

func main() {
	b := []byte("abc")
	b = gomisc.AppendBytes(b, []byte("def"), []byte("ghi"))

	fmt.Println(string(b))
}
abcdefghi

列出目录中的所有文件(包括子目录)

func ListFilesInDir(rootDir string) ([]string, error)
package main

import (
	"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)
	}
}
/home/ligang/tmp/misc/accounts.json
/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文件

func SaveJsonFile(filePath string, v interface{}) error
func ParseJsonFile(filePath string, v interface{}) error
package main

import (
	"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())
	}

	v2 := make(map[string]string)
	err = gomisc.ParseJsonFile(filePath, &v2)
	if err != nil {
		fmt.Println("parse json file failed: " + err.Error())
	}

	ok := true
	for k, v := range v2 {
		if v != v1[k] {
			ok = false
			fmt.Println("save parse json file error, k: " + k + " not equal")
		}
	}
	if ok {
		fmt.Println("save parse json file ok")
	}
}
save parse json file ok

字符串截取

func SubString(str string, start, cnt int) (string, error)
package main

import (
	"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(ss)
}
end too long
end too long
defg

时间格式化定义

const (
	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"
)

// 时间格式:yy-mm-dd H:i:s
func TimeGeneralLayout() string {
	layout := TIME_FMT_STR_YEAR + "-" + TIME_FMT_STR_MONTH + "-" + TIME_FMT_STR_DAY + " "
	layout += TIME_FMT_STR_HOUR + ":" + TIME_FMT_STR_MINUTE + ":" + TIME_FMT_STR_SECOND

	return layout
}

生成随机数

func RandByTime(t *time.Time) int64
package main

import (
	"github.com/goinbox/gomisc"

	"fmt"
	"time"
)

func main() {
	tm := time.Now()
	fmt.Println(gomisc.RandByTime(&tm), gomisc.RandByTime(&tm), gomisc.RandByTime(nil))
}
6537266505970973466 6537266505970973466 5616191991172093652

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


Recommend

  • 42
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的连接池pool

    今天来说下gobox中的连接池底层实现pool 为什么需要连接池 我们的系统在访问外部资源(redis、mysql等)时,为了提高性能,通常会用到的一个优化方法就是把已经使用过的tcp连接保存起来,这样当需要再次使用时,就可...

  • 40
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的log操作

    今天来说下使用gobox中的log操作 log级别定义 const ( LEVEL_EMERGENCY = 0 LEVEL_ALERT = 1 LEVEL_CRITICAL = 2 LEVEL_ERROR = 3 LEVEL_WARNING = 4 LEVEL_NOTICE = 5 LEVEL_INFO = 6...

  • 49
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的http请求处理框架

    今天来说下使用gobox中的http请求处理框架 http请求处理架构图 重要的对象 System system用于实现g...

  • 39
    • blog.7rule.com 6 years ago
    • Cache

    gobox中的consumer处理框架

    我们都会有从异步队列中消费的需求,今天来说下gobox中的consumer处理框架 consumer处理架构图 重要的对象 IM...

  • 40
    • www.cnblogs.com 6 years ago
    • Cache

    Json常用序列化工具包大比拼

    一、前言 Json已成为计算机编程中最常用的数据传输和存储格式之一,所以对Json的序列化和反序列化工具的选择也是互联网系统中比较重要的环节,尤其在高并发下的执行效率,可能会直接影响系统的吞吐率。本文将从功能和性能两方...

  • 13
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的httpclient

    gobox中的httpclient Aug 18, 2018 今天来说下使用gobox中httpclient,这个包就相当于命令行的curl工具,用于发起http请求。 重要的对象 config const ( DEFAUL...

  • 17
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的分页操作

    gobox中的分页操作 Aug 3, 2018 今天来说下使用gobox中的分页操作 分页也是我们开发时的一个常见需求,gobox中提供了page包做这个事情 package main import ( "github....

  • 18
    • blog.7rule.com 4 years ago
    • Cache

    gobox中redis操作

    gobox中redis操作 Jul 29, 2018 今天来说下使用gobox中redis操作相关 本包的driver部分使用了redigo:https://github.com/garyburd/redigo package main import ( "git...

  • 12
    • blog.7rule.com 4 years ago
    • Cache

    gobox中mysql操作

    gobox中mysql操作 Jul 21, 2018 今天来说下使用gobox中mysql操作相关 本包的driver部分使用了go-sql-driver:https://github.com/go-sql-driver/mysql 示例表结构为: | de...

  • 9
    • blog.7rule.com 4 years ago
    • Cache

    gobox中的simplecache和levelcache

    gobox中的simplecache和levelcache Jun 10, 2018 今天来说下gobox中的simplecache和levelcache simplecache simplecache提供了一个简单的内存kv package main i...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK