goutils

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 9 Imported by: 0

README

A Useful Collection Of Go functions.

master build Go Report Card Coverage codeql-analysis Releases LICENSE

A collection of useful functions, require only standard modules.

Note: The main branch may be in an unstable or even broken state during development.

Document at https://pkg.go.dev/github.com/hanke0/goutils

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ToPrettyJSON = ToPrettyJson

ToPrettyJSON likes ToJSON with pretty output

Functions

func Expand

func Expand(s string, mapping func(varName string, default_ string) string) (string, error)

Expand replaces ${var} in the template string base on the mapping function.

It's like os.Expand but with following differences:

  1. strict syntax checks, return the string replaces as many as we can, and an syntax error.
  2. compatible with bash like ${var:=default} syntax.
  3. $var outputs as is.

func InplaceSliceToString

func InplaceSliceToString(s []byte) string

InplaceSliceToString transfer []byte into string inplace

func InplaceStringToSlice

func InplaceStringToSlice(s string) []byte

InplaceStringToSlice transfer string into []byte inplace

func Isin

func Isin(s []string, elem string) bool

Isin return true if the element is in the slice.

func ShortcutUTF8 added in v0.6.0

func ShortcutUTF8(s string, max int, suffix string) string

ShortcutUTF8 return a valid UTF-8 string with at most `max` + len(suffix) characters.

If `s` has more than `max` characters, cuts it to max and add `suffix`. Multibyte characters are handled resonable. If `max` is lower than 0, then return `s`.

func ToJSON

func ToJSON(o interface{}) string

ToJSON return the object json marshal not HTML escaped string. An empty string returned if there is a marshal error.

func ToPrettyJson

func ToPrettyJson(o interface{}) string

ToPrettyJson like ToJSON with pretty output

Types

type MultiErr added in v0.6.0

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

MultiErr represent a bucket of errors. Usually it should be used as follow:

  var m MultiErr
  for _, id := range []string{"1", "2"} {
    id := id
    go func1() {
      err := somefunc1(id)
      m.Set(id, err)
    }
  }

MultiErr all method is goroutine-safe(lock-guarded).

func (*MultiErr) All added in v0.6.0

func (m *MultiErr) All() (all bool)

All returns true if all id's error is nil.

func (*MultiErr) Error added in v0.6.0

func (m *MultiErr) Error() error

Error returns an error. It returns nil if All returns true.

func (*MultiErr) Fails added in v0.6.0

func (m *MultiErr) Fails() (ids []string)

Fails gets failed id list.

func (*MultiErr) Get added in v0.6.0

func (m *MultiErr) Get(id string) (err error)

Get gets the id's error. If id not absent, it returns a nil.

func (*MultiErr) GetE added in v0.6.0

func (m *MultiErr) GetE(id string) (err error, ok bool)

GetE gets the id's error and a bool represent if id exists.

func (*MultiErr) Range added in v0.6.0

func (m *MultiErr) Range(f func(id string, err error) bool)

Range loops errors. It stops loop if `f` returns false.

func (*MultiErr) Set added in v0.6.0

func (m *MultiErr) Set(id string, err error)

Set sets an id with an error.

func (*MultiErr) String added in v0.6.0

func (m *MultiErr) String() string

String gets description of MultiErr.

func (*MultiErr) Successes added in v0.6.0

func (m *MultiErr) Successes() (ids []string)

Successes gets successful id list.

Directories

Path Synopsis
Package assert provide function useful for unit testing.
Package assert provide function useful for unit testing.
Package bloom provides a bloom filter implementation inspired by leveldb.
Package bloom provides a bloom filter implementation inspired by leveldb.
Package hedging provides a method to get an no-error answer from two functions.
Package hedging provides a method to get an no-error answer from two functions.
internal
Package set implements wild-used set container.
Package set implements wild-used set container.
Package strings contains a bucket of operations of string.
Package strings contains a bucket of operations of string.

Jump to

Keyboard shortcuts

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