krylib

package module
v0.0.0-...-2ef208d Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: BSD-2-Clause Imports: 13 Imported by: 31

README

krylib

Random utilities for golang

Documentation

Index

Constants

View Source
const TimestampFormat = "2006-01-02 15:04:05"

TimestampFormat is a commonly used format time values

Variables

View Source
var ErrInvalidValue error = errors.New("invalid value")

ErrInvalidValue Indicates that some value is invalid.

View Source
var ErrNotImplemented error = errors.New("not implemented") // nolint: golint

ErrNotImplemented Indicates that some functionality is not implemented, yet.

Functions

func Chomp

func Chomp(s string) string

Chomp returns a copy of the argument string with all line-ending characters removed.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies the file at src to dst. It copies only the content, not metadata liker ownership, access right, etc.

In case of an error, an incomplete or corrupt file at dst might remain.

func Date

func Date(y, m, d int) time.Time

Date returns a Time value that 00:00:00 local time on the given date.

func ExpandTilde

func ExpandTilde(path string) string

ExpandTilde replaces a leading "~" in a path with the current user's home directory.

func Fexists

func Fexists(path string) (bool, error)

Fexists cchecks if a file exists.

func Fibonacci

func Fibonacci(n int) int64

Fibonacci returns the nth Fibonacci number.

func FileSize

func FileSize(path string) (int64, error)

FileSize returns the size of the given file in bytes. If the file does not exist, it returns an error

func FmtBytes

func FmtBytes(bytes int64) string

FmtBytes formats a number of bytes into a human-readable string, i.e. 1536 becomes "1.5 KB".

func GetHomeDirectory

func GetHomeDirectory() string

GetHomeDirectory determines the user's home directory. The reason this is even a thing is that Unix-like systems store this path in the environment variable "HOME", whereas Windows uses the environment variable "USERPROFILE", Hence this function.

func IntRange

func IntRange(n int) []int

IntRange returns a slice of integers in the range of [0,n) If that sounds primitive, that is because it *is* primitive -- languages like Perl or Ruby offer language primitives for this kind of feature. But Go likes to keep it simple, so here we go.

func IsDir

func IsDir(path string) (bool, error)

IsDir checks if the given path exists and is a Directory. If the given path does not exists, it returns false, nil

func Max

func Max[T Number](x, y T) T

Max returns the greater of two values

func Min

func Min[T Number](x, y T) T

Min returns the smaller of two values.

func Mtime

func Mtime(path string) (time.Time, error)

Mtime returns the mtime stamp of the given file. If the path does not exist, it returns an error

func ParseURL

func ParseURL(s string) *url.URL

ParseURL parses a string containing (hopefully) a URL. Returns the url.URL pointer if successful, panics otherwise. The point is to be able to use this function to initialize "global" variables.

func RandomFilename

func RandomFilename() string

RandommFilename generates filename that is randomized file name that should be random enough to avoid collisions.

CAVEAT: The randomness in these filenames is intended to avoid naming collisions by *accident*. They do *not* offer any kind of protection against a malicious adversary. Needless to say, they are also totally useless for anything that even /remotely/ anything to do with cryptography.

func SplitOnWhitespace

func SplitOnWhitespace(s string) []string

SplitOnWhitespace splits a string on whitespace and returns a slice of the resulting substrings.

func StringSlice

func StringSlice(arr []fmt.Stringer) []string

StringSlice takes a slice of Stringers (i.e. objects that implement the String() method), creates a slice of strings of the same size as the input, then calls String() on each member of the input slice and stores the result in the output slice which it returns at the end.

func Trace

func Trace()

Trace emits info about the CALLING function I found this code on StackOverflow, so the copyright situation is a bit hazy. Then again, if you didn't want people to use your code like I do here, you'd probably not post it in StackOverflow. Ergo I consider this public domain code, since I am now also unable to find the thread where I saw this code. My point is that I did not come up with this myself but I use it happily.

PS: I should readlly give this function a do-over so that it returns

the data it obtains, rather than writing it to Stdout.

func TraceInfo

func TraceInfo() string

TraceInfo returns a string that contains the name of the source file, the line number in the file and the name of the function where TraceInfo is called.

Types

type DateCounter

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

DateCounter is a counter that returns consecutive days.

func NewDateCounter

func NewDateCounter(y, m, d int) *DateCounter

NewDateCounter creates a new date counter that starts counting at the given date.

func (*DateCounter) Next

func (dc *DateCounter) Next() time.Time

Next returns the next date, starting with the date given as the starting date, then adding one day per consecutive call.

func (*DateCounter) Set

func (dc *DateCounter) Set(y, m, d int) time.Time

Set sets the internal counter to the given date.

type ID

type ID int64

ID is an identifier that is unique within a given context.

const INVALID_ID ID = -1

INVALID_ID is an ID that does not equal any valid ID.

type Message

type Message struct {
	Msg   string
	Stamp time.Time
}

Message is a ... message that can sent and received via the MessageBuffer.

func (*Message) StampString

func (m *Message) StampString() string

StampString returns the Message's timestamp as a string.

type MessageBuffer

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

MessageBuffer is a queue-like structure.

func CreateMessageBuffer

func CreateMessageBuffer() *MessageBuffer

CreateMessageBuffer creates a MessageBuffer.

func (*MessageBuffer) AddMessage

func (b *MessageBuffer) AddMessage(msg string)

AddMessage adds a message to the Buffer

func (*MessageBuffer) Count

func (b *MessageBuffer) Count() int

Count returns the number of messages currently queued in the buffer.

func (*MessageBuffer) Empty

func (b *MessageBuffer) Empty() bool

Empty returns true if the queue is currently empty.

func (*MessageBuffer) GetAllMessages

func (b *MessageBuffer) GetAllMessages() []Message

GetAllMessages retrieves all Messages that are currently in the queue.

func (*MessageBuffer) GetOneMessage

func (b *MessageBuffer) GetOneMessage() *Message

GetOneMessage retrieves a single message from the MessageBuffer.

func (*MessageBuffer) Running

func (b *MessageBuffer) Running() bool

Running returns true if the MessgeBuffer is active.

func (*MessageBuffer) Stop

func (b *MessageBuffer) Stop()

Stop tells the message buffer to cease activity.

type Number

type Number interface {
	int | uint | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64
}

Number is the sum type of all integer and floating point types that are strictly ordered.

type Semaphore

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

Semaphore implements a classic among synchronization facilities.

func NewSemaphore

func NewSemaphore(max int) *Semaphore

NewSemaphore creates, uh, a new semaphore with a maximum counter value of max.

func (*Semaphore) Dec

func (s *Semaphore) Dec()

Dec decreases the counter by one, atomically. If the counter is currently zero, it blocks until another goroutine calls Inc() on the same semaphore.

func (*Semaphore) Inc

func (s *Semaphore) Inc()

Inc increases the counter. If the counter was previously zero (0), it will signal the next goroutine waiting on the Semaphore.

Jump to

Keyboard shortcuts

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