go_utils

package module
v0.0.0-...-dce6a1d Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: MIT Imports: 20 Imported by: 0

README

go-utils

My daily toolbox for the Go language.

How to use

import "git.bytewise.xyz/xyz/go-utils"

Remarks

  1. No promise of compatibility. Changes can happen quickly and unexpectedly. Use version tagging if you need stability.

  2. I use my own code style. Don't bother. Don't use if you don't like.

  3. Currently, this repo is not publicly writable. Copy or steal and then use or modify what you want.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFileToZip

func AddFileToZip(zipWriter *zip.Writer, filename string) error

func ContainsString

func ContainsString(list []string, value string) bool

func DirectoryIsEmpty

func DirectoryIsEmpty(path string) (result bool, err error)

func FileSize

func FileSize(filepath string) (int64, error)

func MakeRandomAsciiString

func MakeRandomAsciiString(length int, chars []byte) string

func ReadL10nFile

func ReadL10nFile(filename string, catalog *catalog.Builder) (err error)

catalog = nil -> use defaultcatalog

func StrToInt64

func StrToInt64(s string, defaultvalue int64) int64

func StrToUInt64

func StrToUInt64(s string, defaultvalue uint64) uint64

func Unzip

func Unzip(srcFile string, destPath string) ([]string, error)

Unzip will decompress a zip archive, moving all files and folders within the zip file (parameter 1) to an output directory (parameter 2).

func ZipFiles

func ZipFiles(destFile string, srcFiles ...string) error

ZipFiles compresses one or many files into a single zip archive file. Param 1: filename is the output zip file's name. Param 2: files is a list of files to add to the zip.

Types

type TAverageCounter

type TAverageCounter struct {
	LastValue float64
	Count     uint64
	Mean      float64
	// contains filtered or unexported fields
}

func NewTAverageCounter

func NewTAverageCounter() *TAverageCounter

func (*TAverageCounter) AddValue

func (ac *TAverageCounter) AddValue(value float64)

func (*TAverageCounter) StdDeviation

func (ac *TAverageCounter) StdDeviation() float64

func (*TAverageCounter) Variance

func (ac *TAverageCounter) Variance() float64

type TCache

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

func NewCache

func NewCache(defaultExpiration, cleanupInterval time.Duration) *TCache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func NewCacheFrom

func NewCacheFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]TCachedItem) *TCache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

NewFrom() also accepts an items map which will serve as the underlying map for the cache. This is useful for starting from a deserialized cache (serialized using e.g. gob.Encode() on c.CachedItems()), or passing in e.g. make(map[string]TCachedItem, 500) to improve startup performance when the cache is expected to reach a certain minimum size.

Only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.CachedItems() (subject to the same caveat.)

Note regarding serialization: When using e.g. gob, make sure to gob.Register() the individual types stored in the cache before encoding a map retrieved with c.CachedItems(), and to register those same types before decoding a blob containing an items map.

func (TCache) CachedCount

func (c TCache) CachedCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (TCache) CachedItems

func (c TCache) CachedItems() map[string]TCachedItem

Copies all unexpired items in the cache into a new map and returns it.

func (TCache) Delete

func (c TCache) Delete(key string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (TCache) Find

func (c TCache) Find(findFunc func(data interface{}) bool) (data interface{}, found bool)

Find an item in the cache with a find function. Returns the item or nil, and a bool indicating whether the item was found.

func (TCache) Flush

func (c TCache) Flush()

Delete all items from the cache.

func (TCache) Get

func (c TCache) Get(key string) (data interface{}, found bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (TCache) GetDefault

func (c TCache) GetDefault(key string, defaultdata interface{}) (data interface{})

func (TCache) MemoryConsumedRoughly

func (c TCache) MemoryConsumedRoughly() (membytes int)

func (TCache) OnInsertion

func (c TCache) OnInsertion(f func(key string, insertedItem TCachedItem))

func (TCache) OnNeedData

func (c TCache) OnNeedData(f func(key string) (data interface{}, dontcache bool))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (TCache) OnRemoval

func (c TCache) OnRemoval(f func(key string, removedItem TCachedItem))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (TCache) ReadExportedData

func (c TCache) ReadExportedData(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

func (TCache) ReadFromFile

func (c TCache) ReadFromFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

func (TCache) SaveToFile

func (c TCache) SaveToFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

func (TCache) Set

func (c TCache) Set(key string, data interface{})

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (TCache) WriteExportedData

func (c TCache) WriteExportedData(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

type TCachedItem

type TCachedItem struct {
	Data       interface{}
	Expiration int64
}

type TFileOrDirExistsResult

type TFileOrDirExistsResult byte
const (
	ExistsNothing TFileOrDirExistsResult = iota
	ExistsFile
	ExistsDir
	ExistsOther
	ExistsError
)

func FileOrDirExists

func FileOrDirExists(filename string) TFileOrDirExistsResult

Is filename not exists or a regular file or a directory?

type TLocationInfo

type TLocationInfo struct {
	Name        string
	ParentName  string
	FileName    string
	OffsetToday int
	ZoneName    string // = "" when this is a node
	ID          uint64
	// contains filtered or unexported fields
}

func (TLocationInfo) IsNode

func (li TLocationInfo) IsNode() bool

type TLocationInfos

type TLocationInfos []TLocationInfo

func GetOsTimeZones

func GetOsTimeZones(asTree, OnlyImportant bool) (locationInfos TLocationInfos)

func (TLocationInfos) FilterByParent

func (li TLocationInfos) FilterByParent(parentName string) (result TLocationInfos)

func (TLocationInfos) FindID

func (li TLocationInfos) FindID(id uint64) int

func (TLocationInfos) Len

func (li TLocationInfos) Len() int

func (TLocationInfos) Less

func (li TLocationInfos) Less(i, j int) bool

func (TLocationInfos) Swap

func (li TLocationInfos) Swap(i, j int)

type TStats

type TStats struct {
	// nanosec
	DurationLoadDataToCache, DurationGetDataFromCache TAverageCounter
}

Jump to

Keyboard shortcuts

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