# Go snippets

> Small Go functions for slices, strings and maths, each with a short explanation and an example call.

Canonical: https://www.wiki.jodisand.me/snippets/go/
Reviewed: 2026-09-24
Related: [Go](https://www.wiki.jodisand.me/go/index.md), [Testing](https://www.wiki.jodisand.me/testing/index.md)


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](https://www.wiki.jodisand.me/go/) for idiomatic patterns.


## Pages

- [All](https://www.wiki.jodisand.me/snippets/go/all/index.md): Returns true if all elements in the collection pass the test implemented by the provided function, false otherwise.
- [AllSame](https://www.wiki.jodisand.me/snippets/go/allsame/index.md): Returns true if all elements in the collection have the same value, false otherwise.
- [Any](https://www.wiki.jodisand.me/snippets/go/any/index.md): Returns true if at least one element in the collection passes the test implemented by the provided function, false otherwise.
- [Average](https://www.wiki.jodisand.me/snippets/go/average/index.md): Returns the average of two or more numbers.
- [Capitalize](https://www.wiki.jodisand.me/snippets/go/capitalize/index.md): Capitalizes the first letter of a string.
- [CelsiusToFahrenheit](https://www.wiki.jodisand.me/snippets/go/celsiustofahrenheit/index.md): Converts Celsius to Fahrenheit.
- [Clamp](https://www.wiki.jodisand.me/snippets/go/clamp/index.md): Clamps n within the inclusive range specified by the boundary values a and b.
- [Compact](https://www.wiki.jodisand.me/snippets/go/compact/index.md): Removes zero values from a collection.
- [CompactWhiteSpace](https://www.wiki.jodisand.me/snippets/go/compactwhitespace/index.md): Returns a string with whitespaces compacted.
- [Concat](https://www.wiki.jodisand.me/snippets/go/concat/index.md): Concatenates two slices.
- [ContainsWhiteSpace](https://www.wiki.jodisand.me/snippets/go/containswhitespace/index.md): Returns a string with whitespaces compacted.
- [Decapitalize](https://www.wiki.jodisand.me/snippets/go/decapitalize/index.md): Decapitalizes the first letter of a string.
- [Dedupe](https://www.wiki.jodisand.me/snippets/go/dedupe/index.md): Deduplicates the elements in a given array or slice.
- [Degrees](https://www.wiki.jodisand.me/snippets/go/degrees/index.md): Converts an angle from radians to degrees.
- [Digits](https://www.wiki.jodisand.me/snippets/go/digits/index.md): Converts an integer to an array of digits.
- [FahrenheitToCelsius](https://www.wiki.jodisand.me/snippets/go/fahrenheittocelsius/index.md): Converts Fahrenheit to Celsius.
- [Filter](https://www.wiki.jodisand.me/snippets/go/filter/index.md): Returns a new slice with all elements that pass the test implemented by the provided function.
- [Find](https://www.wiki.jodisand.me/snippets/go/find/index.md): Returns the value of the first element in the provided collection that satisfies the provided testing function.
- [FindIndex](https://www.wiki.jodisand.me/snippets/go/findindex/index.md): Returns the index of the first element in the provided collection that satisfies the provided testing function, -1 otherwise.
- [FindLast](https://www.wiki.jodisand.me/snippets/go/findlast/index.md): Returns the value of the last element in the provided collection that satisfies the provided testing function.
- [FindLastIndex](https://www.wiki.jodisand.me/snippets/go/findlastindex/index.md): Returns the index of the last element in the provided collection that satisfies the provided testing function, -1 otherwise.
- [Frequencies](https://www.wiki.jodisand.me/snippets/go/frequencies/index.md): Returns a map with the unique values of the collection as keys and their frequencies as the values.
- [GCD](https://www.wiki.jodisand.me/snippets/go/gcd/index.md): Calculates the greatest common divisor between two or more numbers.
- [HammingDistance](https://www.wiki.jodisand.me/snippets/go/hammingdistance/index.md): Calculates the Hamming distance between two values.
- [Includes](https://www.wiki.jodisand.me/snippets/go/includes/index.md): Returns true if the given element can be found in the collection, false otherwise.
- [IndentString](https://www.wiki.jodisand.me/snippets/go/indentstring/index.md): Indents each line in the provided string.
- [IndexOf](https://www.wiki.jodisand.me/snippets/go/indexof/index.md): Returns the first index at which a given element can be found in the collection, or -1 if it is not present.
- [IndexOfAll](https://www.wiki.jodisand.me/snippets/go/indexofall/index.md): Returns all indexes of a given element in the collection, or [-1] if it is not present.
- [IntRange](https://www.wiki.jodisand.me/snippets/go/intrange/index.md): Initializes an integer slice in the given range (inclusive).
- [IsEven](https://www.wiki.jodisand.me/snippets/go/iseven/index.md): Returns true if the given number is even, false otherwise.
- [IsInRange](https://www.wiki.jodisand.me/snippets/go/isinrange/index.md): Checks if the given number falls within the given range.
- [IsLower](https://www.wiki.jodisand.me/snippets/go/islower/index.md): Checks if a string is lower case.
- [IsOdd](https://www.wiki.jodisand.me/snippets/go/isodd/index.md): Returns true if the given number is odd, false otherwise.
- [IsPalindrome](https://www.wiki.jodisand.me/snippets/go/ispalindrome/index.md): Returns true if the given string is a palindrome, false otherwise.
- [IsPowerOf2](https://www.wiki.jodisand.me/snippets/go/ispowerof2/index.md): Returns true if the given number is a power of 2, false otherwise.
- [IsUpper](https://www.wiki.jodisand.me/snippets/go/isupper/index.md): Checks if a string is upper case.
- [Join](https://www.wiki.jodisand.me/snippets/go/join/index.md): Returns a string by concatenating all of the elements in the collection, separated by the specified separator string.
- [LCM](https://www.wiki.jodisand.me/snippets/go/lcm/index.md): Returns the least common multiple of two or more numbers.
- [Map](https://www.wiki.jodisand.me/snippets/go/map/index.md): Returns a new slice populated with the results of calling the provided function on every element in the collection.
- [MapNumRange](https://www.wiki.jodisand.me/snippets/go/mapnumrange/index.md): Maps a number from one range to another range.
- [Mask](https://www.wiki.jodisand.me/snippets/go/mask/index.md): Replaces all but the last n of characters with the specified mask character.
- [MaxOf](https://www.wiki.jodisand.me/snippets/go/maxof/index.md): Returns the maximum value of two or more numbers.
- [Median](https://www.wiki.jodisand.me/snippets/go/median/index.md): Returns the median of a collection of numbers.
- [MinOf](https://www.wiki.jodisand.me/snippets/go/minof/index.md): Returns the minimum value of two or more numbers.
- [None](https://www.wiki.jodisand.me/snippets/go/none/index.md): Returns true if no elements in the collection pass the test implemented by the provided function, false otherwise.
- [PadLeft](https://www.wiki.jodisand.me/snippets/go/padleft/index.md): Pads the given string from the start with spaces until the resulting string reaches the given length.
- [PadRight](https://www.wiki.jodisand.me/snippets/go/padright/index.md): Pads the given string from the end with spaces until the resulting string reaches the given length.
- [Rads](https://www.wiki.jodisand.me/snippets/go/rads/index.md): Converts an angle from degrees to radians.
- [RandIntInRange](https://www.wiki.jodisand.me/snippets/go/randintinrange/index.md): Returns a random integer in the specified range.
- [RandIntSliceInRange](https://www.wiki.jodisand.me/snippets/go/randintsliceinrange/index.md): Returns a slice of n random integers in the specified range.
- [ReverseString](https://www.wiki.jodisand.me/snippets/go/reversestring/index.md): Reverses a string
- [Stringify](https://www.wiki.jodisand.me/snippets/go/stringify/index.md): Converts the given value to a string.
- [Sum](https://www.wiki.jodisand.me/snippets/go/sum/index.md): Returns the sum of two or more numbers.
- [ToCamel](https://www.wiki.jodisand.me/snippets/go/tocamel/index.md): Converts a string to kebab case.
- [ToKebab](https://www.wiki.jodisand.me/snippets/go/tokebab/index.md): Converts a string to kebab case.
- [ToSnake](https://www.wiki.jodisand.me/snippets/go/tosnake/index.md): Converts a string to snake case.
- [TruncateString](https://www.wiki.jodisand.me/snippets/go/truncatestring/index.md): Truncates a string up to a specified length.
- [WithIndex](https://www.wiki.jodisand.me/snippets/go/withindex/index.md): Returns a map with index-value pairs.
- [XProduct](https://www.wiki.jodisand.me/snippets/go/xproduct/index.md): Creates a new slice out of the two supplied by creating each possible pair from the collections.
- [Zip](https://www.wiki.jodisand.me/snippets/go/zip/index.md): Creates a collection of elements, grouped based on the position in the original collections.
