utils

package module
v0.0.0-...-0597d20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2018 License: MIT Imports: 23 Imported by: 2

README

go-utils

Golang utils

Documentation

Overview

Example
fmt.Println(PrefixUint32([]byte("id-"), 3))
Output:

[105 100 45 3 0 0 0]

Index

Examples

Constants

View Source
const IDSplitByte = '-'

IDSplitByte split byte

Variables

This section is empty.

Functions

func CheckError

func CheckError(err error)

CheckError check error.

func Concat

func Concat(bs ...[]byte) []byte

Concat bytes.

Example
a := []byte("aa")
b := []byte("bb")
fmt.Println(Concat(a, b))
Output:

[97 97 98 98]

func Contains

func Contains(slice, lambdaOrElement interface{}) bool

Contains is element contains by lambda or element

func Decode

func Decode(bs []byte, obj interface{}) error

Decode is bytes to obj.

Example
var str string
Decode([]byte{5, 12, 0, 2, 'a', 'b'}, &str)
fmt.Println(str)
Output:

ab

func Encode

func Encode(obj interface{}) ([]byte, error)

Encode is obj to bytes.

Example
bs, _ := Encode("ab")
fmt.Printf("%x\n", bs)
Output:

050c00026162

func Filter

func Filter(collection, predicate interface{}) (interface{}, error)

Filter 过滤

func Includes

func Includes(array []string, objs ...string) bool

Includes 数组包含

func Join

func Join(sep []byte, bs ...[]byte) []byte

Join bytes.

Example
a := []byte("aa")
b := []byte("bb")
fmt.Println(Join([]byte(","), a, b))
Output:

[97 97 44 98 98]

func Open

func Open(uri string) error

Open calls the OS default program for uri

func Parse

func Parse(data []string, def map[int]string, p interface{}) (map[string]string, error)

Parse data update p by def

func PrefixBytes

func PrefixBytes(bs []byte, prefix ...byte) []byte

PrefixBytes is prefix add bytes to bytes.

Example
fmt.Println(PrefixBytes([]byte("abc"), 'i', 'd', '-'))
Output:

[105 100 45 97 98 99]

func PrefixUint32

func PrefixUint32(prefix []byte, num uint32) []byte

PrefixUint32 is prefix add uint32 to bytes.

Example
fmt.Println(PrefixUint32([]byte("id-"), 3))
Output:

[105 100 45 3 0 0 0]

func ReadBuf

func ReadBuf(path string, read func([]byte)) error

ReadBuf read file from buf.

func ReadLines

func ReadLines(file string, read func(string)) (err error)

ReadLines read file.

Example
if err := ReadLines("LICENSE", func(line string) {
	fmt.Print(line)
}); err != nil {
	fmt.Println(err)
}
Output:

func Remove

func Remove(slice, lambdaOrElement interface{}) int

Remove element by lambda or element

func SplitAfter

func SplitAfter(s string, sep ...string) []string

SplitAfter is plus of strings.SplitAfter.

Example
fmt.Println(SplitAfter("110120119129", "0", "9"))
Output:

[110 120 119 129]

func UniqueString

func UniqueString(prefix string) string

UniqueString 程序运行期间生成不重复的字符串.

Example
fmt.Println(UniqueString("U-"))
fmt.Println(UniqueString("K-"))
fmt.Println(UniqueString("K-"))
Output:

U-1
K-2
K-3

func UniqueUint32

func UniqueUint32() uint32

UniqueUint32 程序运行期间生成不重复的uint32.

Example
fmt.Println(UniqueUint32())
fmt.Println(UniqueUint32())
Output:

4
5

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is LRU.

func NewCache

func NewCache(expire time.Duration) *Cache

NewCache new cache.

func (*Cache) Close

func (c *Cache) Close()

Close cache.

func (*Cache) Count

func (c *Cache) Count() int

Count Cache.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, bool)

Get value by key.

func (*Cache) Put

func (c *Cache) Put(key, value interface{})

Put vlaue by key.

func (*Cache) Remove

func (c *Cache) Remove(key interface{})

Remove key.

type ChMap

type ChMap struct {
	// contains filtered or unexported fields
}

ChMap is channel map.

func NewChMap

func NewChMap() ChMap

NewChMap new ChMap.

Example
chMap := NewChMap()
defer chMap.Close()
chMap.Put("key", "value")
fmt.Println(chMap.Count())
v, ok := chMap.Get("key")
fmt.Println(v, ok)
fmt.Println(chMap.Has("key"))
fmt.Println(chMap.Has("no key"))
Output:

1
value true
true
false

func (ChMap) Close

func (p ChMap) Close()

Close this ChMap.

func (ChMap) Count

func (p ChMap) Count() int

Count ChMap.

func (ChMap) Get

func (p ChMap) Get(key interface{}) (interface{}, bool)

Get obj by key.

func (ChMap) Has

func (p ChMap) Has(key interface{}) bool

Has key.

func (ChMap) Iterator

func (p ChMap) Iterator(callBack func(k, v interface{}))

Iterator map.

Example
chMap := NewChMap()
defer chMap.Close()
chMap.Put("key", "value")
chMap.Iterator(func(k, v interface{}) {
	fmt.Printf("k: %s, v: %s\n", k, v)
})
Output:

k: key, v: value

func (ChMap) Keys

func (p ChMap) Keys() []interface{}

Keys is get this map keys.

func (ChMap) Put

func (p ChMap) Put(key, value interface{})

Put value by key.

func (ChMap) Remove

func (p ChMap) Remove(key interface{})

Remove obj by key.

type Closer

type Closer interface {
	Close()
}

Closer have Close function.

type FileID

type FileID struct {
	Size int64
	// contains filtered or unexported fields
}

FileID file ID.

func NewFileID

func NewFileID(file string) (*FileID, error)

NewFileID 新建文件ID

func (*FileID) ID

func (f *FileID) ID() []byte

ID 文件ID

func (*FileID) String

func (f *FileID) String() string

func (*FileID) Write

func (f *FileID) Write(data []byte) (int, error)

type ID

type ID [18]byte

ID is unique primary key

func NewID

func NewID(prefix byte) ID

NewID is new ID

Example
fmt.Println(ID{}.IsNew())
id := NewID('A')
fmt.Println(id.IsNew())
Output:

true
false

func (ID) Equal

func (id ID) Equal(other ID) bool

Equal returns a boolean reporting whether id and other

func (ID) IsNew

func (id ID) IsNew() bool

IsNew is New ID

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON is json Marshal

func (*ID) Parse

func (id *ID) Parse(str string) (err error)

Parse is string to ID

func (*ID) ParseBytes

func (id *ID) ParseBytes(bs []byte) error

ParseBytes is bytes to ID

func (ID) String

func (id ID) String() string

String is ID to string

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON is json Unmarshal

type IDS

type IDS []ID

IDS is ID slice.

Example
ids := IDS{}
ids.Add(NewID('A'))

fmt.Println(len(ids))
Output:

1

func (*IDS) Add

func (s *IDS) Add(ids ...ID) []ID

Add push ids to ID slice.

Example
ids := IDS{}
ids.Add(NewID('A'), NewID('A'))

fmt.Println(len(ids))
Output:

2

func (*IDS) Contains

func (s *IDS) Contains(id ID) bool

Contains returns true if id is found, else false.

Example
ids := IDS{}
id := NewID('K')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)

fmt.Println(ids.Contains(id))
fmt.Println(ids.Contains(NewID('A')))
Output:

true
false

func (*IDS) Delete

func (s *IDS) Delete(ids ...ID) []ID

Delete removes all occurrences of ids in the ID slice.

Example
ids := IDS{}
id := NewID('K')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)

fmt.Println(len(ids))
fmt.Println(ids.Contains(id))
fmt.Println(len(ids.Delete(id)))
fmt.Println(ids.Contains(id))
Output:

3
true
2
false

func (*IDS) DeleteIndex

func (s *IDS) DeleteIndex(index ...int) []ID

DeleteIndex removes all occurrences of index in the ID slice.

Example
ids := IDS{}
id := NewID('K')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)

fmt.Println(len(ids))
fmt.Println(ids.Contains(id))
fmt.Println(len(ids.DeleteIndex(1, 2)))
fmt.Println(ids.Contains(id))
Output:

3
true
1
false

func (*IDS) Distinct

func (s *IDS) Distinct() []ID

Distinct returns the new duplicate free slice.

Example
ids := IDS{}
id := NewID('A')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)
ids.Add(NewID('A'), NewID('A'), NewID('A'), NewID('A'))
ids.Add(id)

fmt.Println(len(ids))
ids.Distinct()
fmt.Println(len(ids))
fmt.Println(ids[2] == id)
Output:

8
7
true

func (*IDS) Empty

func (s *IDS) Empty() bool

Empty returns true if ID slice is empty, else false.

Example
ids := IDS{}

fmt.Println(ids.Empty())
ids.Add(NewID('A'))
fmt.Println(ids.Empty())
Output:

true
false

func (*IDS) IndexOf

func (s *IDS) IndexOf(id ID) int

IndexOf returns the index of the matched id, else -1.

Example
ids := IDS{}
id := NewID('K')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)

fmt.Println(ids.IndexOf(id))
Output:

2

func (*IDS) Intersect

func (s *IDS) Intersect(ids ...ID) []ID

Intersect returns the slice of intersecting id. Union see Add Except see Delete

Example
ids := IDS{}
id := NewID('K')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)
ids.Intersect(id, NewID('A'))

fmt.Println(len(ids))
Output:

1

func (*IDS) Reverse

func (s *IDS) Reverse() []ID

Reverse returns the reverse order for ID slice.

Example
ids := IDS{}
id := NewID('A')
ids.Add(NewID('A'), NewID('A'))
ids.Add(id)

ids.Reverse()

fmt.Println(ids[0] == id)
Output:

true

type Result

type Result struct {
	Data  interface{}
	Point interface{}
}

Result 结果.

type Results

type Results struct {
	Data []Result
	Len  int
	Size int
	// contains filtered or unexported fields
}

Results 最优结果集.

func NewResults

func NewResults(size int, less, equal func(i, j interface{}) bool) *Results

NewResults 新建结果集.

func (*Results) Add

func (r *Results) Add(data, point interface{})

Add 增加结果.

func (*Results) AddResults

func (r *Results) AddResults(results *Results)

AddResults 增加结果集.

func (*Results) Get

func (r *Results) Get(i int) (interface{}, interface{})

Get 获取数据.

func (*Results) GetData

func (r *Results) GetData(i int) interface{}

GetData 数据.

func (*Results) GetPoint

func (r *Results) GetPoint(i int) interface{}

GetPoint 得分.

type StringSlice

type StringSlice []string

StringSlice string slice

Example
ss := StringSlice([]string{"123", "234", "234"})

fmt.Println(ss.Delete("234"))
fmt.Println(ss)
Output:

[123]
[123]

func (*StringSlice) Add

func (ss *StringSlice) Add(vals ...string) []string

Add adds all elements of vals in the slice.

Example
ss := StringSlice([]string{"123"})

fmt.Println(ss.Add("234", "345"))
fmt.Println(ss)
Output:

[123 234 345]
[123 234 345]

func (StringSlice) Contains

func (ss StringSlice) Contains(val string) bool

Contains returns true if val is found, else false.

Example
ss := StringSlice([]string{"123", "234"})

fmt.Println(ss.Contains("234"))
fmt.Println(ss.Contains("345"))
Output:

true
false

func (*StringSlice) Delete

func (ss *StringSlice) Delete(vals ...string) []string

Delete removes all occurrences of vals in the slice.

Example
ss := StringSlice([]string{"123", "234", "234"})

fmt.Println(ss.Delete("345"))
fmt.Println(ss.Delete("234"))
fmt.Println(ss)
Output:

[123 234 234]
[123]
[123]

func (*StringSlice) IndexOf

func (ss *StringSlice) IndexOf(val string) int

IndexOf returns the index of the matched val, else -1.

Example
ss := StringSlice([]string{"123", "234"})

fmt.Println(ss.IndexOf("234"))
fmt.Println(ss.IndexOf("345"))
Output:

1
-1

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL