- Package basic provides some utility functions.
- Package str provides some utilities to manipulate string.
- Package arr provides some utilities to manipulate array.
- Package ref provides some utilities to manipulate reflect.
go get github.com/qw20012/go-basic
Get the exact value of any.
var anyMap = map[string]any{"1": 1}
if FromAny[int](anyMap["1"]) != 1 {
t.Fatalf("TestFromAny failed")
}
Make sure any type is created. Create by reflect if it is not there.
var emptyAnyMap map[string]any
fromEmptyAnyMap := basic.NewIfEmpty(emptyAnyMap)
if fromEmptyAnyMap == nil {
t.Fatalf("GetOrCreate with emtpy map failed")
}
fromEmptyAnyMap["key"] = 1
Represents the emptry string.
if str.Empty != "" {
t.Fatal("IsEmpty failed")
}
Identify whether the source string is empty.
if IsEmpty("abc") {
t.Fatal("IsEmpty failed " + "abc")
}
Identify whether the source string is empty.
if !IsNotEmpty("abc") {
t.Fatal("IsNotEmpty failed" + "abc")
}
Contact the sources from any type.
twoDiffType := Contact("abc", 1)
if twoDiffType != "abc1" {
t.Fatal("TestContact failed " + "abc, 1")
}
Convert to string from any type.
f := From(1.123)
if f != "1.123" {
t.Fatal("From failed " + "1.123")
}
Format source string that instead given name in curly brackets by given value.
diffTypeValue := Format("abc {name}", "name", 1)
if diffTypeValue != "abc 1" {
t.Fatal("TestFormat failed " + "abc, 1 ")
}
Format source string by calling Format functon. See also Format.
strFromMap := Formats("{a}{ b }c", diffTypeValue)
if strFromMap != "Dog1c" {
t.Fatal("TestFormats failed " + "Dog1c")
}
Identify whether the given array contians the given element.
var aArray = [3]int{1, 2, 3}
if !Contains(aArray[:], 2) {
t.Fatalf("TestContains failed")
}
Get the exact value of given reflect.Value.
if GetValue[int](reflect.ValueOf(1)) != 1 {
t.Fatalf("TestGetValue with int type failed")
}
Identify whether the any type is nil.
var emptyArray [1]int
if basic.IsNil(emptyArray) {
t.Fatalf("GetOrCreate with emtpy slice failed")
}
Identify whether the any type is zero.
var emptyArray [1]int
if !IsZero(emptyArray) {
t.Fatalf("TestIsZero with emtpy slice failed")
}
PRs accepted.
BSD-style © Barret Qin