package checkfn
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strings"
)
func IsNil (v any ) bool {
if v == nil {
return true
}
rv := reflect .ValueOf (v )
switch rv .Kind () {
case reflect .Chan , reflect .Func , reflect .Interface , reflect .Map , reflect .Ptr , reflect .Slice :
return rv .IsNil ()
default :
return false
}
}
func IsSimpleKind (k reflect .Kind ) bool {
if reflect .String == k {
return true
}
return k > reflect .Invalid && k <= reflect .Float64
}
func IsEqual (src , dst any ) bool {
if src == nil || dst == nil {
return src == dst
}
bs1 , ok := src .([]byte )
if !ok {
return reflect .DeepEqual (src , dst )
}
bs2 , ok := dst .([]byte )
if !ok {
return false
}
if bs1 == nil || bs2 == nil {
return bs1 == nil && bs2 == nil
}
return bytes .Equal (bs1 , bs2 )
}
func Contains (data , elem any ) (valid , found bool ) {
if data == nil {
return false , false
}
dataRv := reflect .ValueOf (data )
dataRt := reflect .TypeOf (data )
dataKind := dataRt .Kind ()
if dataKind == reflect .String {
return true , strings .Contains (dataRv .String (), fmt .Sprint (elem ))
}
if dataKind == reflect .Map {
mapKeys := dataRv .MapKeys ()
for i := 0 ; i < len (mapKeys ); i ++ {
if IsEqual (mapKeys [i ].Interface (), elem ) {
return true , true
}
}
return true , false
}
if dataKind != reflect .Slice && dataKind != reflect .Array {
return false , false
}
for i := 0 ; i < dataRv .Len (); i ++ {
if IsEqual (dataRv .Index (i ).Interface (), elem ) {
return true , true
}
}
return true , false
}
func StringsContains (ss []string , sub string ) bool {
for _ , v := range ss {
if v == sub {
return true
}
}
return false
}
var (
numReg = regexp .MustCompile (`^[-+]?\d*\.?\d+$` )
pNumReg = regexp .MustCompile (`^\d*\.?\d+$` )
)
func IsNumeric (s string ) bool {
if s == "" {
return false
}
return numReg .MatchString (s )
}
func IsPositiveNum (s string ) bool {
if s == "" {
return false
}
if s [0 ] == '-' {
return false
}
return pNumReg .MatchString (s )
}
func IsHttpURL (s string ) bool {
return strings .HasPrefix (s , "http://" ) || strings .HasPrefix (s , "https://" )
}
func IndexByteAfter (s string , b byte , startIndex int ) int {
idx := strings .IndexByte (s [startIndex :], b )
if idx < 0 {
return -1
}
return idx + startIndex
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .