Snippets
Go snippets
Small, self-contained Go functions. Each one shows the implementation, a short explanation of how it works and an example call.
- All Returns true if all elements in the collection pass the test implemented by the provided function, false …
- AllSame Returns true if all elements in the collection have the same value, false otherwise. Use reflect.ValueOf() to …
- Any Returns true if at least one element in the collection passes the test implemented by the provided function, …
- Average Returns the average of two or more numbers. Use range to iterate over the values of nums, adding each value to …
- Capitalize Capitalizes the first letter of a string. Use strings.ToUpper() to capitalize the first letter of the string. …
- CelsiusToFahrenheit Converts Celsius to Fahrenheit. Follows the conversion formula F = 1.8C + 32. func CelsiusToFahrenheit(d …
- Clamp Clamps n within the inclusive range specified by the boundary values a and b. If n falls within the range, …
- Compact Removes zero values from a collection. Use reflect.ValueOf() to get the array or slice, make() to make an …
- CompactWhiteSpace Returns a string with whitespaces compacted. Use Regexp.ReplaceAllString() with a regular expression to …
- Concat Concatenates two slices. Implement an appropriate function for each type. Use append() and the fact that …
- ContainsWhiteSpace Returns a string with whitespaces compacted. Use Regexp.MatchString() with a regular expression to check if …
- Decapitalize Decapitalizes the first letter of a string. Use strings.ToLower() to decapitalize the first letter of the …
- Dedupe Deduplicates the elements in a given array or slice. Implement an appropriate function for each type. Use …
- Degrees Converts an angle from radians to degrees. Use math.Pi and the radian to degree formula to convert the angle …
- Digits Converts an integer to an array of digits. Use strconv.Itoa() to convert the given number to a string, make() …
- FahrenheitToCelsius Converts Fahrenheit to Celsius. Follows the conversion formula C = (F - 32) * 5/9. func FahrenheitToCelsius(d …
- Filter Returns a new slice with all elements that pass the test implemented by the provided function. Implement an …
- Find Returns the value of the first element in the provided collection that satisfies the provided testing …
- FindIndex Returns the index of the first element in the provided collection that satisfies the provided testing …
- FindLast Returns the value of the last element in the provided collection that satisfies the provided testing function. …
- FindLastIndex Returns the index of the last element in the provided collection that satisfies the provided testing function, …
- Frequencies Returns a map with the unique values of the collection as keys and their frequencies as the values. Implement …
- GCD Calculates the greatest common divisor between two or more numbers. Define a gcd() function for two numbers, …
- HammingDistance Calculates the Hamming distance between two values. Use the XOR operator (^) to find the bit difference …
- Includes Returns true if the given element can be found in the collection, false otherwise. Use reflect.ValueOf() to …
- IndentString Indents each line in the provided string. Use strings.Replace() to prepend i to each line. import …
- IndexOf Returns the first index at which a given element can be found in the collection, or -1 if it is not present. …
- IndexOfAll Returns all indexes of a given element in the collection, or [-1] if it is not present. Use reflect.ValueOf() …
- IntRange Initializes an integer slice in the given range (inclusive). Use make() to create a slice of appropriate size. …
- IsEven Returns true if the given number is even, false otherwise. Checks whether a number is odd or even using the …
- IsInRange Checks if the given number falls within the given range. Use math.Min() and math.Max() to determine the start …
- IsLower Checks if a string is lower case. Use strings.ToLower() to convert the string to lower case and compare it to …
- IsOdd Returns true if the given number is odd, false otherwise. Checks whether a number is odd or even using the …
- IsPalindrome Returns true if the given string is a palindrome, false otherwise. Use strings.Fields(), strings.Join() and …
- IsPowerOf2 Returns true if the given number is a power of 2, false otherwise. Use the bitwise binary AND operator (&) …
- IsUpper Checks if a string is upper case. Use strings.ToUpper() to convert the string to upper case and compare it to …
- Join Returns a string by concatenating all of the elements in the collection, separated by the specified separator …
- LCM Returns the least common multiple of two or more numbers. Define a gcd() function that determines the greatest …
- Map Returns a new slice populated with the results of calling the provided function on every element in the …
- MapNumRange Maps a number from one range to another range. Returns num mapped between oMin-oMax from iMin-iMax. func …
- Mask Replaces all but the last n of characters with the specified mask character. Slice the given string …
- MaxOf Returns the maximum value of two or more numbers. Use math.Inf(-1) to set the initial maximum value to …
- Median Returns the median of a collection of numbers. Find the midpoint and convert it to an integer, use …
- MinOf Returns the minimum value of two or more numbers. Use math.Inf(1) to set the initial minimum value to positive …
- None Returns true if no elements in the collection pass the test implemented by the provided function, false …
- PadLeft Pads the given string from the start with spaces until the resulting string reaches the given length. Use …
- PadRight Pads the given string from the end with spaces until the resulting string reaches the given length. Use …
- Rads Converts an angle from degrees to radians. Use math.Pi and the degree to radian formula to convert the angle …
- RandIntInRange Returns a random integer in the specified range. Use rand.Intn() to generate a random number between 0 and the …
- RandIntSliceInRange Returns a slice of n random integers in the specified range. Use make() to create an appropriate slice. Use …
- ReverseString Reverses a string Use make() to create an appropriate rune slice. Use range and len() to iterate over the …
- Stringify Converts the given value to a string. Use fmt.Sprintf() with the "%v" format specifier to convert …
- Sum Returns the sum of two or more numbers. Use range to iterate over the values of nums, adding each value to …
- ToCamel Converts a string to kebab case. Use range and strings.Fields() to iterate over the words in the string. Use …
- ToKebab Converts a string to kebab case. Use strings.Fields() to get the words in the string, strings.Join() to …
- ToSnake Converts a string to snake case. Use strings.Fields() to get the words in the string, strings.Join() to …
- TruncateString Truncates a string up to a specified length. Use len() to determine if the length is greater than l. Return …
- WithIndex Returns a map with index-value pairs. Use reflect.ValueOf() to get the array or slice, make() to create a new …
- XProduct Creates a new slice out of the two supplied by creating each possible pair from the collections. Use …
- Zip Creates a collection of elements, grouped based on the position in the original collections. Use range to …
No snippet matches that filter.