utils

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 16 Imported by: 4

Documentation

Index

Constants

View Source
const (
	B int64 = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
)

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir coyies a dir.

func CopyFile

func CopyFile(src string, dst string) (n int64, err error)

CopyFile copies a file.

func GetLocalIPList added in v0.1.1

func GetLocalIPList() (list []string, err error)

GetLocalIPList return the list of local ip address.

func MustEncodeGob

func MustEncodeGob(v interface{}) []byte

func MustEncodeJSON added in v0.5.5

func MustEncodeJSON(v interface{}) []byte

func NormalizePathname added in v0.9.0

func NormalizePathname(pathname string) string

NormalizePathname is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func ParseBytes

func ParseBytes(s string) (int64, error)

ParseBytes parses a bytes string

func ParseGobFile

func ParseGobFile(filename string, v interface{}) (err error)

func ParseJSONFile

func ParseJSONFile(filename string, v interface{}) (err error)

func SplitByFirstByte

func SplitByFirstByte(s string, c byte) (string, string)

SplitByFirstByte splits string by a char from the beginning.

func SplitByLastByte

func SplitByLastByte(s string, c byte) (string, string)

SplitByLastByte splits string by a char from the end.

func ToNumber

func ToNumber(v any) (f float64, err error)

ToNumber covert a 'number' interface to float64.

func WaitForExitSignal added in v0.6.3

func WaitForExitSignal(callback func(os.Signal) bool)

WaitForExitSignal waits for the exit signal.

func WriteGobFile added in v0.1.1

func WriteGobFile(filename string, v interface{}) (err error)

func WriteJSONFile added in v0.1.1

func WriteJSONFile(filename string, v interface{}, indent string) (err error)

func Zip added in v0.9.0

func Zip(path string, w io.Writer) error

Zip compresses the path to the io.Writer.

Types

type KeyedMutex added in v0.9.5

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

KeyedMutex is a mutex that locks on a key.

func (*KeyedMutex) Lock added in v0.9.5

func (m *KeyedMutex) Lock(key string) func()

Lock locks the mutex for the given key.

type KeyedMutexItem added in v0.9.5

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

KeyedMutexItem is an item in a KeyedMutex.

type Once added in v0.9.5

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

Once is an object that will perform exactly one action. Different from sync.Once, this implementation allows the once function to return an error, that doesn't update the done flag.

func (*Once) Do added in v0.9.5

func (o *Once) Do(f func() error) error

Do calls the function f if and only if Do is being called for the first time for this instance of Once

type ReadOnlySet added in v0.9.5

type ReadOnlySet[T comparable] struct {
	// contains filtered or unexported fields
}

ReadOnlySet is a read-only set.

func NewReadOnlySet added in v0.9.5

func NewReadOnlySet[T comparable](values ...T) *ReadOnlySet[T]

NewReadOnlySet creates a new read-only set with the given values.

func (*ReadOnlySet[T]) Has added in v0.9.5

func (s *ReadOnlySet[T]) Has(value T) bool

Has returns true if the set contains the given value.

func (*ReadOnlySet[T]) Len added in v0.9.5

func (s *ReadOnlySet[T]) Len() int

Len returns the number of elements in the set.

func (*ReadOnlySet[T]) Values added in v0.9.5

func (s *ReadOnlySet[T]) Values() []T

Values returns all the values in the set.

type Set added in v0.9.5

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a set.

func NewSet added in v0.9.5

func NewSet[T comparable](values ...T) *Set[T]

NewSet creates a new set with the given values.

func (*Set[T]) Add added in v0.9.5

func (s *Set[T]) Add(value T)

Add adds the given value to the set.

func (*Set[T]) Has added in v0.9.5

func (s *Set[T]) Has(value T) bool

Has returns true if the set contains the given value.

func (*Set[T]) Len added in v0.9.5

func (s *Set[T]) Len() int

Len returns the number of elements in the set.

func (*Set[T]) ReadOnly added in v0.9.6

func (s *Set[T]) ReadOnly() *ReadOnlySet[T]

ReadOnly returns a read-only version of the set.

func (*Set[T]) Remove added in v0.9.5

func (s *Set[T]) Remove(value T)

Remove removes the given value from the set.

func (*Set[T]) Reset added in v0.9.5

func (s *Set[T]) Reset()

Reset removes all the values from the set.

func (*Set[T]) Values added in v0.9.5

func (s *Set[T]) Values() []T

Values returns all the values in the set.

Jump to

Keyboard shortcuts

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