Go snippets
Small Go functions for slices, strings and maths, each with a short explanation and an example call.
Short third-party examples with no input validation or error handling, and some predate generics, so several repeat one function per element type. Check the edge cases and write a test before using one. See Go for idiomatic patterns.
- AllReturns true if all elements in the collection pass the test implemented by the provided function, false otherwise.
- AllSameReturns true if all elements in the collection have the same value, false otherwise.
- AnyReturns true if at least one element in the collection passes the test implemented by the provided function, false otherwise.
- AverageReturns the average of two or more numbers.
- CapitalizeCapitalizes the first letter of a string.
- CelsiusToFahrenheitConverts Celsius to Fahrenheit.
- ClampClamps n within the inclusive range specified by the boundary values a and b.
- CompactRemoves zero values from a collection.
- CompactWhiteSpaceReturns a string with whitespaces compacted.
- ConcatConcatenates two slices.
- ContainsWhiteSpaceReturns a string with whitespaces compacted.
- DecapitalizeDecapitalizes the first letter of a string.
- DedupeDeduplicates the elements in a given array or slice.
- DegreesConverts an angle from radians to degrees.
- DigitsConverts an integer to an array of digits.
- FahrenheitToCelsiusConverts Fahrenheit to Celsius.
- FilterReturns a new slice with all elements that pass the test implemented by the provided function.
- FindReturns the value of the first element in the provided collection that satisfies the provided testing function.
- FindIndexReturns the index of the first element in the provided collection that satisfies the provided testing function, -1 otherwise.
- FindLastReturns the value of the last element in the provided collection that satisfies the provided testing function.
- FindLastIndexReturns the index of the last element in the provided collection that satisfies the provided testing function, -1 otherwise.
- FrequenciesReturns a map with the unique values of the collection as keys and their frequencies as the values.
- GCDCalculates the greatest common divisor between two or more numbers.
- HammingDistanceCalculates the Hamming distance between two values.
- IncludesReturns true if the given element can be found in the collection, false otherwise.
- IndentStringIndents each line in the provided string.
- IndexOfReturns the first index at which a given element can be found in the collection, or -1 if it is not present.
- IndexOfAllReturns all indexes of a given element in the collection, or [-1] if it is not present.
- IntRangeInitializes an integer slice in the given range (inclusive).
- IsEvenReturns true if the given number is even, false otherwise.
- IsInRangeChecks if the given number falls within the given range.
- IsLowerChecks if a string is lower case.
- IsOddReturns true if the given number is odd, false otherwise.
- IsPalindromeReturns true if the given string is a palindrome, false otherwise.
- IsPowerOf2Returns true if the given number is a power of 2, false otherwise.
- IsUpperChecks if a string is upper case.
- JoinReturns a string by concatenating all of the elements in the collection, separated by the specified separator string.
- LCMReturns the least common multiple of two or more numbers.
- MapReturns a new slice populated with the results of calling the provided function on every element in the collection.
- MapNumRangeMaps a number from one range to another range.
- MaskReplaces all but the last n of characters with the specified mask character.
- MaxOfReturns the maximum value of two or more numbers.
- MedianReturns the median of a collection of numbers.
- MinOfReturns the minimum value of two or more numbers.
- NoneReturns true if no elements in the collection pass the test implemented by the provided function, false otherwise.
- PadLeftPads the given string from the start with spaces until the resulting string reaches the given length.
- PadRightPads the given string from the end with spaces until the resulting string reaches the given length.
- RadsConverts an angle from degrees to radians.
- RandIntInRangeReturns a random integer in the specified range.
- RandIntSliceInRangeReturns a slice of n random integers in the specified range.
- ReverseStringReverses a string
- StringifyConverts the given value to a string.
- SumReturns the sum of two or more numbers.
- ToCamelConverts a string to kebab case.
- ToKebabConverts a string to kebab case.
- ToSnakeConverts a string to snake case.
- TruncateStringTruncates a string up to a specified length.
- WithIndexReturns a map with index-value pairs.
- XProductCreates a new slice out of the two supplied by creating each possible pair from the collections.
- ZipCreates a collection of elements, grouped based on the position in the original collections.
No snippet matches that filter.