Some commonly used tools in development
The name "dfo" is derived from the homophonic sound of "default".
This package can safely load pointer values and replace empty pointers with default values.
goget-ugithub.com/liang09255/lutils/dfoExample:
nums1, nums2:=1, 1addTwoNum:=func(a*int, b*int) int {
// This is a service which can add two numbersreturndfo.IntPtr(a) +dfo.IntPtr(b)
}
addTwoNum(nil, nil) // 0 dfo turn nil to 0addTwoNum(&nums1, nil) // 1addTwoNum(nil, &nums2) // 1addTwoNum(&nums1, &nums2) // 2Without dfo, we will implement the function like this, dfo can make the code look cleaner
addTwoNum:=func(a*int, b*int) int {
num1, num2:=0, 0ifa!=nil {
num1=*a }
ifb!=nil {
num2=*b
}
returnnum1+num2
}This package can convert the type of a variable to another type.
We can use conv.SetLogger to set logger, conv will automatically collect errors into logger
goget-ugithub.com/liang09255/lutils/convExample:
str:="1.0"i1:=conv.ToInt(str) // 1 (int)ui1:=conv.ToUint(str) // 1 (uint)f1:=conv.ToFloat64(str) // 1 (float64)str="true"b:=conv.ToBool(str) // true (bool)m:=map[string]string{"a": "b"}
jsonContent:=conv.ToJSON(m) // {"a":"b"}arr:= []string{"a", "b", "c"}
m1:=conv.ArrayToMap(arr) // map[a:{} b:{} c:{}]_, ok1:=m1["a"] // ok1 => true_, ok2:=m1["d"] // ok2 => falsem2:=conv.ArrayToBoolMap(arr)
ok3:=m2["a"] // trueok4:=m2["d"] // false// use loggerconv.SetLogger(Logger)
// logger need to implement interface Logger// type Logger interface {// Info(args ...interface{})// Warn(args ...interface{})// Error(args ...interface{})// }