helper

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Overview

Package helper provides some helpful functions.

Index

Examples

Constants

View Source
const (
	Day  = time.Hour * 24
	Week = Day * 7
)

Some common durations.

Variables

View Source
var (
	CommentHash    = []byte("#")
	CommentSlashes = []byte("//")
)

Pre-define some comment characters.

View Source
var (
	NumCharset      = "0123456789"
	HexCharset      = NumCharset + "abcdefABCDEF"
	HexLowerCharset = NumCharset + "abcdef"
	HexUpperCharset = NumCharset + "ABCDEF"

	AlphaLowerCharset = "abcdefghijklmnopqrstuvwxyz"
	AlphaUpperCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	AlphaCharset      = AlphaLowerCharset + AlphaUpperCharset

	AlphaNumLowerCharset = NumCharset + AlphaLowerCharset
	AlphaNumUpperCharset = NumCharset + AlphaUpperCharset
	AlphaNumCharset      = NumCharset + AlphaCharset

	DefaultCharset = AlphaNumLowerCharset
)

Pre-define some charsets to generate the random string.

View Source
var IntN = rand.Intn

IntN is the alias of the function "math/rand.Intn" or "math/rand/v2.IntN".

Functions

func Bytes added in v0.42.0

func Bytes(s string) []byte

Bytes converts the value s from string to []byte.

NOTICE: s must not be modified.

func DecodeJSONFromBytes added in v0.30.0

func DecodeJSONFromBytes(dst interface{}, b []byte) error

DecodeJSONFromBytes decodes the json raw bytes s into dst.

DEPRECATED!!!

func DecodeJSONFromString added in v0.30.0

func DecodeJSONFromString(dst interface{}, s string) error

DecodeJSONFromString decodes the json raw string s into dst.

DEPRECATED!!!

func EncodeJSON added in v0.30.3

func EncodeJSON(w io.Writer, value interface{}) error

EncodeJSON encodes the value by json into w.

NOTICE: it does not escape the problematic HTML characters.

func EncodeJSONBytes added in v0.30.3

func EncodeJSONBytes(value interface{}) (data []byte, err error)

EncodeJSONBytes encodes the value by json to bytes.

NOTICE: it does not escape the problematic HTML characters.

DEPRECATED!!!

func EncodeJSONString added in v0.30.3

func EncodeJSONString(value interface{}) (data string, err error)

EncodeJSONString encodes the value by json to string.

NOTICE: it does not escape the problematic HTML characters.

DEPRECATED!!!

func IsZero added in v0.31.0

func IsZero(value interface{}) bool

IsZero reports whether the value is ZERO.

If the value has implemented the interface{ IsZero() bool }, it will be called.

func MakeSlice added in v0.42.0

func MakeSlice[S ~[]E, E any, I ~int | ~int64](len, cap, defaultCap I) S

MakeSlice returns a new slice.

If both cap and defaultCap are equal to 0, it is equal to make(S, len). If cap is equal to 0, use defaultCap as cap instead, which is equal to make(S, len, defaultCap).

DEPRECATED!!!

func MapKeys added in v0.46.0

func MapKeys[M ~map[K]V, K comparable, V any](maps M) []K

MapKeys returns all the keys of the map.

DEPRECATED!!!

func MapKeysFunc added in v0.47.0

func MapKeysFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(K) T) []T

MapKeysFunc returns all the keys of the map by the conversion function.

DEPRECATED!!!

Example
type Key struct {
	K string
	V int32
}
maps := map[Key]bool{
	{K: "a", V: 1}: true,
	{K: "b", V: 2}: true,
	{K: "c", V: 3}: true,
}

keys := MapKeysFunc(maps, func(k Key) string { return k.K })
slices.Sort(keys)
fmt.Println(keys)
Output:

[a b c]

func MapValues added in v0.46.0

func MapValues[M ~map[K]V, K comparable, V any](maps M) []V

MapValues returns all the values of the map.

DEPRECATED!!!

func MapValuesFunc added in v0.47.0

func MapValuesFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(V) T) []T

MapValues returns all the values of the map by the conversion function.

DEPRECATED!!!

Example
type Value struct {
	V int
}
maps := map[string]Value{
	"a": {V: 1},
	"b": {V: 2},
	"c": {V: 3},
}

values := MapValuesFunc(maps, func(v Value) int { return v.V })
slices.Sort(values)
fmt.Println(values)
Output:

[1 2 3]

func MergeSlice added in v0.43.0

func MergeSlice[S ~[]E, E any](ss ...S) S

MergeSlice merges a set of slices in turn to one slice.

If no arguments, return nil. If all the arguments are empty, return a empty slice with cap==0.

DEPRECATED!!!

func Must added in v0.35.0

func Must(err error)

Must panics if err is not nil

func MustV added in v0.35.0

func MustV[T any](value T, err error) T

MustV returns value if err is nil. Or, panic with err instead.

DEPRECATED!!!

func Random added in v0.39.0

func Random(buf []byte, charset string)

Random generates a random string with the length from the given charsets into buf.

func RandomString added in v0.8.0

func RandomString(n int, charset string) string

RandomString generates a random string with the length from the given charsets.

func RemoveLineComments added in v0.45.0

func RemoveLineComments(data, comments []byte) []byte

RemoveLineComments is a simple function to remove the whole line comment that the first non-white character starts with comments, and the similar line tail comment.

Example
var slashOrig = []byte(`
// line comment 1
1
    /// line comment 2
2   ///// line tail comment 3
3
    4
	"//": "abc"
	"abc"  // the trailling comment containing "
5
`)

var hashOrig = []byte(`
# line comment 1
1
	## line comment 2
2   #### line tail comment 3
3
    4
	"#": "abc"
	"abc"  # the trailling comment containing "
5
`)

fmt.Println("Hash Result:")
fmt.Println(string(RemoveLineComments(hashOrig, CommentHash)))

fmt.Println("Slash Result:")
fmt.Println(string(RemoveLineComments(slashOrig, CommentSlashes)))
Output:

Hash Result:
1
2
3
    4
	"#": "abc"
	"abc"
5

Slash Result:
1
2
3
    4
	"//": "abc"
	"abc"
5

func String added in v0.42.0

func String(b []byte) string

String converts the value b from []byte to string.

NOTICE: b must not be modified.

func TimeAdd added in v0.30.0

func TimeAdd(t time.Time, years, months, days, hours, minutes, seconds int) time.Time

TimeAdd adds a duration to t and returns a new time.Time.

Example
t1 := time.Date(1, 2, 3, 4, 5, 6, 0, time.UTC)
t2 := TimeAdd(t1, 6, 5, 4, 3, 2, 1)

fmt.Printf("Year: %d\n", t2.Year())
fmt.Printf("Month: %d\n", t2.Month())
fmt.Printf("Day: %d\n", t2.Day())
fmt.Printf("Hour: %d\n", t2.Hour())
fmt.Printf("Minute: %d\n", t2.Minute())
fmt.Printf("Second: %d\n", t2.Second())
Output:

Year: 7
Month: 7
Day: 7
Hour: 7
Minute: 7
Second: 7

func ToBoolMap added in v0.47.0

func ToBoolMap[S ~[]T, T comparable](s S) map[T]bool

ToBoolMap converts a slice s to a bool map.

DEPRECATED!!!

Example
boolmap := ToBoolMap([]string{"a", "b", "c"})
fmt.Println(boolmap)
Output:

map[a:true b:true c:true]

func ToBoolMapFunc added in v0.47.0

func ToBoolMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]bool

ToBoolMapFunc converts a slice s to a bool map by a conversion function.

DEPRECATED!!!

Example
type S struct {
	K string
	V int32
}

values := []S{{K: "a", V: 1}, {K: "b", V: 2}, {K: "c", V: 3}}
setmap := ToBoolMapFunc(values, func(s S) string { return s.K })
fmt.Println(setmap)
Output:

map[a:true b:true c:true]

func ToMap added in v0.48.0

func ToMap[S ~[]E, K comparable, V, E any](s S, convert func(E) (K, V)) map[K]V

DEPRECATED!!!

func ToMapWithIndex added in v0.48.0

func ToMapWithIndex[S ~[]E, K comparable, V, E any](s S, convert func(int, E) (K, V)) map[K]V

DEPRECATED!!!

func ToSetMap added in v0.47.0

func ToSetMap[S ~[]T, T comparable](s S) map[T]struct{}

ToSetMap converts a slice s to a set map.

DEPRECATED!!!

Example
setmap := ToSetMap([]string{"a", "b", "c"})
fmt.Println(setmap)
Output:

map[a:{} b:{} c:{}]

func ToSetMapFunc added in v0.47.0

func ToSetMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]struct{}

ToSetMapFunc converts a slice s to a set map by a conversion function.

DEPRECATED!!!

Example
type S struct {
	K string
	V int32
}

values := []S{{K: "a", V: 1}, {K: "b", V: 2}, {K: "c", V: 3}}
setmap := ToSetMapFunc(values, func(s S) string { return s.K })
fmt.Println(setmap)
Output:

map[a:{} b:{} c:{}]

Types

This section is empty.

Jump to

Keyboard shortcuts

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