Collection of small reusable Go functions
Install with
go get github.com/fegoa89/goutils
View contents
View contents
View contents
Import
import "github.com/fegoa89/goutils/string"
Capitalizes the first letter of a string.
Capitalize("hello") // Hello
Examples
Capitalize("my string") // My string
Capitalizes string excluding words such as "and", "a", "with", "or". You can provide an optional string slice with words that will be excluded too
CapitalizeTitle("foo and bar") // Foo and Bar
Convert string to UpperCamelCase
UpperCamelCase("foo and bar") // FooAndBar
Examples
UpperCamelCase("foo and bar") // FooAndBar
UpperCamelCase("foo.and.bar") // FooAndBar
UpperCamelCase("foo-and-bar") // FooAndBar
UpperCamelCase("foo_and_bar") // FooAndBar
UpperCamelCase("Foo and bar") // FooAndBar
Convert string to LowerCamelCase
LowerCamelCase("foo and bar") // fooAndBar
Examples
LowerCamelCase("foo and bar") // fooAndBar
LowerCamelCase("foo.and.bar") // fooAndBar
LowerCamelCase("foo-and-bar") // fooAndBar
LowerCamelCase("foo_and_bar") // fooAndBar
LowerCamelCase("Foo and bar") // fooAndBar
Convert string to to SnakeCase
ToSnakeCase("fooAndBar") // foo_and_bar
Examples
ToSnakeCase("fooAndBar") // foo_and_bar
ToSnakeCase("foo.AndBar") // foo_and_bar
ToSnakeCase("Foo-And-Bar") // foo_and_bar
ToSnakeCase("foo And Bar") // foo_and_bar
ToSnakeCase("你好") // 你_好
Convert string to ToKebabCase
ToKebabCase("fooAndBar") // foo-and-bar
Examples
ToKebabCase("fooAndBar") // foo-and-bar
ToSnakeCase("foo.AndBar") // foo-and-bar
ToSnakeCase("Foo-And-Bar") // foo-and-bar
ToSnakeCase("foo And Bar") // foo-and-bar
ToSnakeCase("你好") // 你-好
Converts a computerized string into a human-friendly one
Humanize("fooAndBar") // foo and bar
Examples
Humanize("fooAndBar") // foo and bar
Humanize("foo_and_bar") // foo and bar
Humanize(" foo-and-bar") // foo and bar
Truncates a string up to a specified string length
Truncate("King Gizzard & The Lizard Wizard", 15) // "King Gizzard..."
Examples
Truncate("En un lugar de la Mancha, de cuyo nombre no quiero acordarme", 27) // "En un lugar de la Mancha..."
Returns true if string is empty or blank
IsEmptyOrBlank("") // true
Examples
IsEmptyOrBlank("") // true
IsEmptyOrBlank(" ") // true
IsEmptyOrBlank("my string") // false
Returns true if string is blank
IsBlank("") // true
Examples
IsBlank("") // true
IsBlank(" ") // true
IsBlank("my string") // false
Returns true if string is empty
IsEmpty("") // true
Examples
IsEmpty("") // true
IsEmpty(" ") // false
IsEmpty("my string") // false
Deletes all whitespaces from a String
DeleteWhitespace("foo and bar") // fooandbar
Examples
DeleteWhitespace("my string") // mystring
DeleteWhitespace(" my string ") // mystring
Return true if both strings are case insensitive equal
CaseInsensitiveEquals("my string", "My String") // true
Examples
CaseInsensitiveEquals("my string", "My String") // true
CaseInsensitiveEquals("my string", "My-String") // false
Determines whether the string is a boolean representation
IsBoolean("true") // true
Examples
IsBoolean("false") // true
IsBoolean("I am a dog") // false
Determines whether the string is a integer representation
IsInteger("1") // true
Examples
IsInteger("344") // true
IsInteger("I am a cat") // false
Determines whether the string is a float representation
IsFloat("1.4") // true
Examples
IsFloat("34.4") // true
IsFloat("nothing") // false
Returns index of the specified character or substring in a particular String
IndexOf("dog", "o") // 1
Examples
IndexOf("dog", "o") // 1
IndexOf("airport", "po") // 3
Removes the accents from a string, converting them to their non-accented corresponding characters.
RemoveAccents("ÀÁÂÃÄÅ") // AAAAAA
Examples
RemoveAccents("ÀÁÂÃÄÅ") // AAAAAA
RemoveAccents("ÒÓÔÕÖØỐṌṒ") // OOOOOØOOO
converts a string to an MD5 hash
GetMD5Hash("hola") // 4d186321c1a7f0f354b297e8914ab240
Transforms a list of words into a word series, returning a string containing all words separated by commas and the conjunction
UnifyWordSeries([]string{"dogs", "cats", "pikachus"}, "and") // dogs, cats and pikachus
Examples
UnifyWordSeries([]string{"dogs"}, "and") // dogs
UnifyWordSeries([]string{"dogs", "cats"}, "or") // dogs or cats
UnifyWordSeries([]string{"dogs", "cats"}, "and") // dogs and cats
Import
import "github.com/fegoa89/goutils/slice"
Adds an elements to the end of a slice and returns the new length of the slice.
mySlice := []interface{}{}
SlicePush(&mySlice, "hello") // 1
Removes the last element from an slice and returns that removed element
mySlice := []interface{}{"red", "fox"}
SlicePop(&mySlice) // "fox"
Removes the first element from an slice and returns that removed element.
mySlice := []interface{}{"red", "fox"}
SliceShift(&mySlice) // "red"
Removes an item by index position and returns the new length of the slice.
mySlice := []interface{}{"red", "fox"}
RemoveItemByIndex(&mySlice, 0) // "1"
Assigns to the slice a duplicate-free version of itself.
mySlice := []interface{}{"red", "red", "fox"}
SliceUnique(&mySlice) // "1"
Returns a slice containing all the entries from slice1 that are not present in slice2.
firstSlice := []interface{}{"red", "dog", "fox"}
secondSlice := []interface{}{"red", "fox"}
SliceDiff(&firstSlice, &secondSlice) // []interface{}{"dog"}
Returns a slice containing all the entries from slice1 that are present in slice2.
firstSlice := []interface{}{"red", "dog", "fox"}
secondSlice := []interface{}{"dog"}
SliceIntersect(&firstSlice, &secondSlice) // []interface{}{"dog"}
Import
import "github.com/fegoa89/goutils/timespan"
Returns the start time.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.Start() // 2015-11-11 00:00:00 +0000 UTC
Returns the end time.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.End() // 2015-11-11 16:00:00 +0000 UTC
Checks if the start date is older than end date.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.ValidDateRange() // true
Returns the quantity of days between two time objects.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 14 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.DaysBetween() // 33
Returns the quantity of hours between two time objects.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.HoursBetween() // 16
Returns the quantity of minutes between two time objects.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.MinutesBetween() // 16
Returns the quantity of seconds between two time objects.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.SecondsBetween() // 57600
Returns the quantity of months between two time objects.
startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 12 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.MonthsBetween() // 1