Documentation ¶
Overview ¶
Package assists provides some assisted functions.
Index ¶
- func ConvertAddr(netaddr net.Addr) (addr netip.Addr)
- func IP2Addr(ip net.IP) (addr netip.Addr)
- func StructFieldNameFuncWithTags(tags ...string) func(reflect.StructField) (name string, arg string)
- func TrimPort(host string) string
- type RuleValidateFunc
- type RuleValidator
- type StructValidateFunc
- type StructValidator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertAddr ¶ added in v0.11.0
ConvertAddr converts the address from net.Addr to netip.Addr.
If failed, return ZERO.
func IP2Addr ¶ added in v0.12.0
IP2Addr converts net.IP to netip.Addr, which also converts 4in6 to ipv4.
If ip is invalid, the returned addr is also invalid.
func StructFieldNameFuncWithTags ¶
func StructFieldNameFuncWithTags(tags ...string) func(reflect.StructField) (name string, arg string)
StructFieldNameFuncWithTags returns a function to get the name and arg of struct field from the given tags in turn until a tag is found.
func TrimPort ¶ added in v0.13.0
TrimPort trims the port from the host if exists. Or, returns itself.
host may be one of formats as follow:
"ipv4" "ipv6" "domain" "ipv4:port" "[ipv6]:port" "domain:port"
Any that is not an ipv4 or ipv6 is as a domain.
NOTICE: it does not validate whether the host is valid.
Example ¶
fmt.Println(TrimPort("")) fmt.Println(TrimPort("[abc")) fmt.Println(TrimPort("[abc]")) fmt.Println(TrimPort("[abc]:80")) // We just trim it by the lexical rule, not check its validity. fmt.Println(TrimPort("[ff00::]:80")) fmt.Println(TrimPort("ff00::")) fmt.Println(TrimPort("1.2.3.4")) fmt.Println(TrimPort("1.2.3.4:80")) fmt.Println(TrimPort("localhost")) fmt.Println(TrimPort("localhost:80"))
Output: [abc abc abc ff00:: ff00:: 1.2.3.4 1.2.3.4 localhost localhost
Types ¶
type RuleValidateFunc ¶
RuleValidateFunc is a rule validation function.
type RuleValidator ¶
RuleValidator is used to validate whether a value conforms with the rule.
type StructValidateFunc ¶
StructValidateFunc is a struct validation function.
func (StructValidateFunc) Validate ¶
func (f StructValidateFunc) Validate(structvalue any) error
Validate implements the interface Validator.
type StructValidator ¶
StructValidator is used to validate whether a struct value is valid.