Documentation
¶
Overview ¶
Package pocket provides utility functions for common patterns that aren't in the Go standard library but are useful to have in your pocket.
Index ¶
- func AssertContains(t *testing.T, got string, substr string)
- func AssertEqual[T any](t *testing.T, a T, b T)
- func AssertErrorIs(t *testing.T, got error, want error)
- func AssertFalse(t *testing.T, got bool)
- func AssertNil(t *testing.T, got any)
- func AssertNotEqual[T any](t *testing.T, a T, b T)
- func AssertNotNil(t *testing.T, got any)
- func AssertPanics(t *testing.T, f func())
- func AssertTrue(t *testing.T, got bool)
- func ConfigDir() (string, error)
- func DataDir() (string, error)
- func Filter[T any](slice []T, f func(T) bool) []T
- func GenerateString(len int) string
- func HomeDir() (string, error)
- func LoadConfigFromEnv[T any]() (*T, error)
- func Map[T any, U any](slice []T, f func(T) U) []U
- func SafeAdd[T Int](a T, b T) T
- func SafeCompare(token1, token2 string) bool
- func SafeDiv[T Int](a T, b T) T
- func SafeMul[T Int](a T, b T) T
- func SafeSub[T Int](a T, b T) T
- func TrySafeAdd[T Int](a T, b T) (T, error)
- func TrySafeDiv[T Int](a T, b T) (T, error)
- func TrySafeMul[T Int](a T, b T) (T, error)
- func TrySafeSub[T Int](a T, b T) (T, error)
- type Int
- type Money
- func (m Money) Amount() int64
- func (m Money) Currency() string
- func (m Money) Dec(amount int64) (Money, error)
- func (m Money) DividedBy(divisor int64) (Money, error)
- func (m Money) Equals(other Money) bool
- func (m Money) Format() string
- func (m Money) Inc(amount int64) (Money, error)
- func (m Money) Minus(other Money) (Money, error)
- func (m Money) Plus(other Money) (Money, error)
- func (m Money) Precision() int
- func (m Money) String() string
- func (m Money) Times(amount int64) (Money, error)
- type Signed
- type Unsigned
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertContains ¶
AssertContains asserts that the given string contains the given substring.
func AssertEqual ¶
AssertEqual asserts that the given values are equal. It uses reflection to do a deep comparison.
func AssertErrorIs ¶
AssertErrorIs asserts that the given error is of the given type. It uses the errors.Is to do the comparison, checking for wrapped errors.
func AssertFalse ¶
AssertFalse asserts that the given value is false.
func AssertNotEqual ¶
AssertNotEqual asserts that the given values are not equal. It uses reflection to do a deep comparison.
func AssertNotNil ¶
AssertNotNil asserts that the given value is not nil.
func AssertPanics ¶
AssertPanics asserts that the given function panics.
func AssertTrue ¶
AssertTrue asserts that the given value is true.
func ConfigDir ¶
ConfigDir returns the configuration directory of the current user. On Unix, it looks for XDG_CONFIG_HOME, defaults to $HOME/.config. On Windows, it checks APPDATA.
func DataDir ¶
DataDir returns the data directory of the current user. On Unix, it looks for XDG_DATA_HOME, defaults to $HOME/.local/share. On Windows, it checks LOCALAPPDATA.
func Filter ¶
Filter applies the given function to each element of the slice and returns a new slice with the elements for which the function returns true.
func GenerateString ¶
GenerateString generates a random string of the specified length. If for any reason `rand.Read` fails, this function will panic!
func HomeDir ¶
HomeDir returns the home directory of the current user. On Unix, it looks for HOME, defaults to the current user's home directory. On Windows, it checks USERPROFILE first, then HOME, then HOMEDRIVE+HOMEPATH.
func LoadConfigFromEnv ¶
LoadConfigFromEnv returns a config struct populated with environment variables.
It uses the `env` struct tag to determine the environment variable name and the `default` tag to determine the default value if the environment variable is not set. It casts the value to the type specified in the struct field.
Example:
type AppConfig struct {
Port int `env:"PORT" default:"8080"`
LogLevel string `env:"LOG_LEVEL"`
Timeout time.Duration `env:"TIMEOUT" default:"10s"`
}
config, err := pocket.LoadConfigFromEnv[AppConfig]()
func Map ¶
Map applies the given function to each element of the slice and returns a new slice with the results.
func SafeAdd ¶
func SafeAdd[T Int](a T, b T) T
SafeAdd returns the sum of two integers, panicking if the result overflows.
func SafeCompare ¶
SafeCompare performs a constant-time comparison of two strings to protect against timing attacks. It hashes both strings to ensure they have the same length.
func SafeDiv ¶
func SafeDiv[T Int](a T, b T) T
SafeDiv return the division of two integers, panicking if the result overflows.
func SafeMul ¶
func SafeMul[T Int](a T, b T) T
SafeMul returns the product of two integers, panicking if the result overflows.
func SafeSub ¶
func SafeSub[T Int](a T, b T) T
SafeSub returns the difference of two integers, panicking if the result overflows.
func TrySafeAdd ¶
TrySafeAdd returns the sum of two integers. Returns an error if the result would overflow or underflow.
func TrySafeDiv ¶
TrySafeDiv returns the division of two integers. Returns an error if the result would overflow or if dividing by zero.
func TrySafeMul ¶
TrySafeMul returns the product of two integers. Returns an error if the result would overflow.
func TrySafeSub ¶
TrySafeSub returns the difference of two integers. Returns an error if the result would overflow or underflow.
Types ¶
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money represents a monetary value. A Money instance is immutable, operations return a new Money instance. Precision is limited to 8 digits to accommdate fairly large values without overflowing.
func NewMoneyFromString ¶
NewMoneyFromString creates a new Money instance from a string. The string must be in the format "amount currency". The number of decimal places determines the precision. So be sure to include 0s if necessary. And be careful not to use unsanitized user input as "100 USD" will be different from "100.00 USD". e.g., "100.99 USD" // precision=2 e.g., "100.00 ARS" // precision=2 e.g., "100 ARS" // precision=0, not what you want for ARS (and most FIAT currencies) e.g., "1.00000000 BTC" // precision=8
func (Money) Amount ¶
Returns the amount of money in the smallest unit of the currency. For example, if money is `Money{amount: 10099, currency: "USD"}`, the amount will be 10099.
func (Money) DividedBy ¶
DividedBy returns a new Money instance with the amount divided by the given divisor. Uses half-up rounding: fractions >= 0.5 round up, < 0.5 round down.
func (Money) Equals ¶
Equals returns true if the two moneys have the same amount, currency, and precision.
func (Money) Minus ¶
Minus returns a new Money with the difference of the two amounts. Returns an error if the currencies don't match or if overflow occurs.
func (Money) Plus ¶
Plus returns a new Money with the sum of the two amounts. Returns an error if the currencies don't match or if overflow occurs.