GOsc is an helper package for Go, written to be user friendly with alias and simple examples.
If you need a new helper, please open an issue and if you have already the code, thank you!
Install the package from your terminal with go get github.com/danilopolani/gosc
and then import it in your project: import "github.com/danilopolani/gosc"
.
- Some / Any - Check if at least one item of the given slice satisfies the given function.
- Every / All - Check if all items of the given slice satisfy the given function.
- Map - Apply the given function to the given slice.
- Filter - Filter out to the given slice the items that don't satisfy the given function.
- Index - Find the index of an item in the given slice.
- Indexi - Find the index of an item in the given slice. (Case Insenstive)
- Delete - Delete an item from a slice.
- Rsort - Reverse the order (desc) of an ordered slice.
- EqSlices - Check if two slices are equal.
- SliceRand - Retrieve a random item from the given slice.
- InSlice - Check if a value is in the given slice.
- ToBytes - Convert a string into a bytes slice.
- ByteToString - Convert a bytes slice into a string.
- Rstring - Reverse a string (every character).
- LcFirst - Convert the first character to the string to LowerCase.
- UcFirst - Convert the first character to the string to UpperCase.
- ToSnake - Convert a string to snake_case.
- ToCamel - Convert a string to camelCase.
- ToPascal - Convert a string to PascalCase.
- ToKebab - Convert a string to kebab-case (aka slug).
- ToInt - Convert a string to an int.
- ToInt64 - Convert a string to an int64.
- ToUint - Convert a string to a uint.
- ToBase64 - Encode a string in base64.
- FromBase64 - Decode a string from base64.
- IsBool - Check if a string is a boolean.
- IsEmail - Check if a string is an email address.
- IsURL - Check if a string is a valid URL.
- IsJSON - Check if a string is a valid JSON document.
- IsIP - Check if a string is an IPv4.
- IsHexColor - Check if a string is a hex color.
- IsRGBColor - Check if a string is a RGB color.
- IsCreditCard - Check if a string is a valid credit card.
- IsOnlyDigits - Check if a string contains only numbers.
- IsOnlyLetters - Check if a string contains only letters.
- IsOnlyAlphaNumeric - Check if a string contains only letters and numbers.
- Uniq - Generate a unique token based on current time and hashed in sha256.
- StrRand - Generate a random string of the given size.
- UUID - Generate a UUID v4 according to RFC 4122.
- IsInt - Check if a string is an integer.
- IsFloat - Check if a string is a float number and numbers.
- Utoa - Transform a uint into a string.
- Rand - Pick a random int from the given range.
- Slice reduce
- Slice unique
- Slice shuffle
- Map key exists
- Map keys
- Map values
The detailed list of helpers with examples.
Check if at least one item of the given slice satisfies the given function.
Methods: SomeString
, SomeInt
, SomeFloat
Alias: AnyString
, AnyInt
, AnyFloat
slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}
fmt.Println(SomeString(slice1, func(s string) bool {
return strings.HasPrefix(s, "ba")
})) // true
fmt.Println(AnyInt(slice2, func(i int) bool {
return i%2 == 0
})) // false
Check if all items of the given slice satisfy the given function.
Methods: EveryString
, EveryInt
, EveryFloat
Alias: AllString
, AllInt
, AllFloat
slice1 := []string{"bar", "baz"}
slice2 := []int{0, 2, 5}
fmt.Println(EveryString(slice1, func(s string) bool {
return strings.HasPrefix(s, "ba")
})) // true
fmt.Println(AllInt(slice2, func(i int) bool {
return i%2 == 0
})) // false
Apply the given function to the given slice.
Methods: MapString
, MapInt
, MapFloat
slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}
fmt.Println(MapString(slice1, strings.ToUpper) // [FOO BAR BAZ]
fmt.Println(MapInt(slice2, func(i int) int {
return i*2
})) // [6 10]
Filter out to the given slice the items that don't satisfy the given function.
Methods: FilterString
, FilterInt
, FilterFloat
slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}
fmt.Println(FilterString(slice1, func(s string) bool {
return strings.HasPrefix(s, "ba")
})) // [bar baz]
fmt.Println(FilterInt(slice2, func(i int) bool {
return i%2 == 0
})) // []
Find the index of an item in the given slice.
Return: int
(-1
if not found)
slice1 := []string{"foo", "bar", "baz"}
slice2 := []int{3, 5}
fmt.Println(Index(&slice1, "baz")) // 2
fmt.Println(Index(&slice2, 6) // -1
Find the index of an item in the given slice. (Case Insensitive)
Return: int
(-1
if not found)
slice1 := []string{"foo", "BAR", "baz"}
fmt.Println(Index(&slice1, "BaR")) // 1
Delete an item from a slice.
Supported types: string
, int
, float64
slice1 := []string{"foo", "bar", "lazy", "dog"}
slice2 := []int{5, -3, 64}
Delete(&slice1, 2)
Delete(&slice2, 0)
fmt.Println(slice1) // [foo bar dog]
fmt.Println(slice2) // [-3 64]
Reverse the order (desc) of an ordered slice.
Alias: ReverseSort
slice1 := []string{"foo", "bar", "lazy", "dog"}
slice2 := []int{5, -3, 64}
Rsort(&slice1)
ReverseSort(&slice2)
fmt.Println(slice1) // [lazy foo dog bar]
fmt.Println(slice2) // [64 5 -3]
Check if two slices are equal (not in depth, use reflect.DeepEqual
for that).
Return: bool
slice1 := []string{"foo", "bar"}
slice2 := []int{5, -3, 64}
fmt.Println(EqSlices(&slice1, &[]string{"foo","bar"})) // true
fmt.Println(EqSlices(&slice2, &slice1)) // false
fmt.Println(EqSlices(&slice2, &[]int{-3, 5, 64})) // false
fmt.Println(EqSlices(&slice2, &[]int{5, -3, 64})) // true
Retrieve a random item from the given slice. If you don't want to assign it to a variable, please look the other functions at the end.
slice1 := []string{"foo", "bar", "lazy", "dog"}
slice2 := []int{5, -3, 64, 777}
var randomString string
var randomInt int
SliceRand(&slice1, &randomString)
SliceRand(&slice2, &randomInt)
fmt.Println(randomString) // My output: bar
fmt.Println(randomInt) // My output: -3
Check if a value is in the given slice.
slice1 := []string{"foo", "bar", "lazy", "dog"}
slice2 := []int{5, -3, 64, 777}
fmt.Println(InSlice("lazy", &slice1)) // frue
fmt.Println(InSlice(55, &slice2)) // false
Convert a string into a bytes slice.
Return: []byte
fmt.Println(gosc.ToBytes("Foo")) // [70 111 111]
Convert a bytes slice into a string.
Return: string
fmt.Println(gosc.ByteToString([]byte{70, 111, 111})) // Foo
Reverse a string (every character).
alias: ReverseString
Return: string
fmt.Println(gosc.Rstring("foo")) // oof
fmt.Println(gosc.ReverseString("소주")) // 주소
Convert the first character to the string to LowerCase.
alias: LowerFirst
Return: string
fmt.Println(gosc.LcFirst("Foo")) // foo
fmt.Println(gosc.LowerFirst("소주")) // 소주
Convert the first character to the string to UpperCase.
alias: UpperFirst
Return: string
fmt.Println(gosc.UcFirst("foo")) // Foo
fmt.Println(gosc.UpperFirst("소주")) // 소주
Convert a string to snake_case.
alias: ToSnakeCase
Return: string
fmt.Println(gosc.ToSnake("Foo 123")) // foo_123
fmt.Println(gosc.ToSnakeCase("camelCase")) // camel_case
Convert a string to camelCase.
alias: ToCamelCase
Return: string
Known bugs:
- [] Doesn't lowercase the string
fmt.Println(gosc.ToCamel("foo bar")) // fooBar
fmt.Println(gosc.ToCamelCase("kebab-case")) // kebabCase
Convert a string to PascalCase.
alias: ToPascalCase
Return: string
Known bugs:
- [] Doesn't lowercase the string
fmt.Println(gosc.ToPascal("foo bar")) // FooBar
fmt.Println(gosc.ToPascalCase("kebab-case")) // KebabCase
Convert a string to kebab-case.
alias: ToKebabCase
Return: string
fmt.Println(gosc.ToKebab("Foo bAr")) // foo-bar
fmt.Println(gosc.ToKebabCase("snake_case")) // snake-case
Convert a string to an int.
Return: int
fmt.Println(gosc.ToInt("-53")) // -53
fmt.Println(gosc.ToInt("542.8")) // 542
fmt.Println(gosc.ToInt("foo")) // 0
Convert a string to an int64.
Return: int64
fmt.Println(gosc.ToInt64("-53")) // -53
fmt.Println(gosc.ToInt64("542.8")) // 542
fmt.Println(gosc.ToInt64("foo")) // 0
Convert a string to a uint.
Return: uint
fmt.Println(gosc.ToUint("2")) // 2
fmt.Println(gosc.ToUint("-53")) // 0
fmt.Println(gosc.ToUint("542.8")) // 542
fmt.Println(gosc.ToUint("foo")) // 0
Encode a string in base64.
Return: string
fmt.Println(gosc.ToBase64("abc〩")) // YWJj44Cp
Decode a string from base64.
Return: string
fmt.Println(gosc.FromBase64("YWJj44Cp")) // abc〩
fmt.Println(gosc.FromBase64("fake")) // ""
Check if a string is a boolean.
Return: bool
fmt.Println(gosc.IsBool("false")) // true
fmt.Println(gosc.IsBool("foo")) // false
fmt.Println(gosc.IsBool("5")) // false
fmt.Println(gosc.IsBool("1")) // true
Check if a string is an email address.
Return: bool
fmt.Println(gosc.IsEmail("me@example.com")) // true
fmt.Println(gosc.IsEmail("foo")) // false
fmt.Println(gosc.IsEmail("")) // false
Check if a string is a valid URL.
Return: bool
Known bugs:
- [] Other protocols (such as FTP) return true
- [] Just the procotol returns true
fmt.Println(gosc.IsURL("https://github.com")) // true
fmt.Println(gosc.IsURL("https://github")) // false
fmt.Println(gosc.IsURL("https://github.com?q=gosc")) // true
Check if a string is a valid JSON document.
Return: bool
fmt.Println(gosc.IsJSON("{\"foo\":\"bar\"}")) // true
fmt.Println(gosc.IsJSON("")) // false
fmt.Println(gosc.IsJSON("[1]")) // true
Check if a string is an IPv4.
Return: bool
fmt.Println(gosc.IsIP("127.0.0.1")) // true
fmt.Println(gosc.IsIP("")) // false
fmt.Println(gosc.IsIP("127.0.0.1.1")) // false
fmt.Println(gosc.IsIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) // false
fmt.Println(gosc.IsIP("D8-D3-85-EB-12-E3")) // false
Check if a string is a hex color.
Return: bool
fmt.Println(gosc.IsHexColor("#fff")) // true
fmt.Println(gosc.IsHexColor("fff")) // true
fmt.Println(gosc.IsHexColor("#ffff")) // false
fmt.Println(gosc.IsHexColor("#ggg")) // false
fmt.Println(gosc.IsHexColor("rgb(255, 255, 255)")) // false
Check if a string is a RGB color.
alias: IsRGB
Return: bool
fmt.Println(gosc.IsRGBColor("rgb(255, 255, 255)")) // true
fmt.Println(gosc.IsRGBColor("rgb(255, 255, 256)")) // false (out of range)
fmt.Println(gosc.IsRGBColor("rgb(255, 255)")) // false
fmt.Println(gosc.IsRGB("rgba(255, 255, 255, 1)")) // false
fmt.Println(gosc.IsRGB("#fff")) // false
Check if a string is a valid credit card.
Return: bool
fmt.Println(gosc.IsCreditCard("4653 0343 2480 9848")) // true (Visa)
fmt.Println(gosc.IsCreditCard("4653-0343-2480-9848")) // true
fmt.Println(gosc.IsCreditCard("1111-0343-2480-9848")) // false (not valid vendor)
fmt.Println(gosc.IsCreditCard("5321 7873 3201 8954")) // true (Mastercard)
fmt.Println(gosc.IsCreditCard("3421-941266-26371")) // true (American Express)
fmt.Println(gosc.IsCreditCard("주주주주-주주주주-주주주주-주주주주")) // false
Check if a string contains only numbers.
alias: IsOnlyNumbers
Return: bool
fmt.Println(gosc.IsOnlyDigits("1234")) // true
fmt.Println(gosc.IsOnlyDigits("-1")) // false
fmt.Println(gosc.IsOnlyNumbers("foo")) // false
Check if a string contains only letters.
alias: IsOnlyAlpha
Return: bool
fmt.Println(gosc.IsOnlyLetters("foo")) // true
fmt.Println(gosc.IsOnlyLetters("foo3")) // false
fmt.Println(gosc.IsOnlyAlpha("주")) // false
fmt.Println(gosc.IsOnlyAlpha("")) // false
Check if a string contains only letters and numbers.
alias: IsOnlyLettersNumbers
, IsOnlyAlphaNum
Return: bool
fmt.Println(gosc.IsOnlyAlphaNumeric("foo")) // true
fmt.Println(gosc.IsOnlyAlphaNumeric("foo3")) // true
fmt.Println(gosc.IsOnlyAlpha("주")) // false
fmt.Println(gosc.IsOnlyAlpha("")) // false
Generate a unique token based on current time and hashed in sha256.
alias: Unique
Return: string
fmt.Println(gosc.Uniq()) // My output: 839079123b9223727c31982cedc4e63f880a91a26591532f08185453d20db497
Generate a random string of the given size.
alias: RandStr
, RandomString
Return: string
fmt.Println(gosc.StrRand(12)) // My output: BbikfUNZdXCS
Generate a UUID v4 according to RFC 4122.
Return: string
fmt.Println(gosc.UUID()) // My output: 8e67e7ae-d674-4470-9345-35511fed974a
Check if a string is an integer.
Return: bool
fmt.Println(gosc.IsInt("5")) // true
fmt.Println(gosc.IsInt("-3")) // true
fmt.Println(gosc.IsInt("76.9")) // false
fmt.Println(gosc.IsInt("foo")) // false
Check if a string is a float number.
Return: bool
fmt.Println(gosc.IsFloat("76.9")) // true
fmt.Println(gosc.IsFloat("5")) // true
fmt.Println(gosc.IsFloat("foo")) // false
Transform a uint into a string.
Return: string
var i uint = 5
fmt.Println(gosc.Utoa(i)) // "5"
Pick a random int from the given range.
Return: int
fmt.Println(Rand(1, 999)) // My output: 508