A small sets of golang indpendent packages for daily usage in any project.
go get github.com/ubgo/goutil
FuncName will return the current function's name. It can be used for a better log debug system.(I'm NOT sure.)
goutil.FuncName() // output: myfunc
Limit the decimal value e.g. 2.34343434 will become 2.34 if precision is set to 2
goutil.FloatPrecision(2.34343434, 2) // output: 2.34
Concat FirstName and LastName
goutil.FullName("Lucian", "Khanakia") // output: Lucian Khanakia
Env get key environment variable if exist otherwise return defalutValue
goutil.Env("MODE", "local") // output: local if set MODE=prod is not set in terminal
It generates random string Define n as number to limit the length of the random string.
goutil.RandString(10) // output: Xydere12sw
Convert any string to snakecase.
goutil.SnakeCase("Lucian Khanakia") // output: luci_khanakia
goutil.SnakeCase("kebab-case") // output: kebab_case
goutil.SnakeCase("Id0Value") // output: id0_value
goutil.SnakeCase("ID0Value") // output: id0_value
goutil.SnakeCase("HTTPRequest") // output: http_request
Convert any struct to JSON string
type Student struct {
Name string `json:"name"`
RoleNo int `json:"roleNo"`
}
student := Student{
Name: "Lucian",
RoleNo: 7,
}
fmt.Println(goutil.ToJSON(student))
// out: {"name":"Lucian","roleNo":7}
fmt.Println(goutil.ToJSONIndent(student))
// out:
{
"name": "Lucian",
"roleNo": 7
}
goutil.PrintToJSON(student)
// out:
{
"name": "Lucian",
"roleNo": 7
}
Use Make the unused value used so golang will not give error while compiling
goutil.Use("Luci")
goutil.Use([]string{"test"})
Check if value exist on a given string slice then return the index
result, ok := StringIndex([]string{"luci", "aman", "khanakia"}, "aman")
// output: 1, true
Check if value exist on a given uint slice then return the index
result, ok := UintIndex([]uint{1, 2, 3}, 2)
// output: 1, true
Check if value exist on a given int slice then return the index
result, ok := IntIndex([]int{1, 2, 3}, 2)
// output: 1, true
Clean the string by removing all the special chars and other weird chars and returns alphanumeric only
result := CleanString("##khanakia")
// output: khanakia
Convert string to sha256
result := HashString("luci")
// output: 2fdcbc8615c275ffbe49106cf85fbab1566b92559a251a5535a217f211dfa3f2
If you would like to contribute to the project, please fork it and send us a pull request. Please add tests for any new features or bug fixes.
- Author - Aman Khanakia
- Website - https://khanakia.com
goutil is MIT licensed.