Returns a string by concatenating all of the elements in the collection, separated by the specified separator string.

Use reflect.ValueOf() to get the array or slice and the separator string, make() to create an appropriate string slice.
Use a for loop with Value.Len() and Value.Index() to iterate over each element, fmt.Sprintf() to convert it to a string.
Use strings.Join() to combine the strings using the separator provided.

继续阅读 30秒学会 Golang 片段 – Join

Removes zero values from a collection.

Use reflect.ValueOf() to get the array or slice, make() to make an appropriate slice.
Use a for loop with Value.Len() and Value.Index() to iterate over each element.
Use append() to add it to the resulting slice if Value.IsZero() is `false.

继续阅读 30秒学会 Golang 片段 – Compact

Returns all indexes of a given element in the collection, or [-1] if it is not present.

Use reflect.ValueOf() to get the array or slice and the value to search for, make() to create an appropriate slice.
Use a for loop with Value.Len() and Value.Index() to iterate over each element and compare it to the search value.
Use append() to add indexes of matching elements to the resulting slice.

继续阅读 30秒学会 Golang 片段 – IndexOfAll

Creates a collection of elements, grouped based on the position in the original collections.

Use range to iterate over the params, reflect.ValueOf() and Value.Len() to find the longest collection.
Use make() to create a 2D interface{} slice of length equal to the longest collection.
Use make() to create a slice for each element in the result, range to iterate over params.
Use reflect.ValueOf(), Value.Len(), append(), Value.Index() and Value.Interface() to add values to the result.

继续阅读 30秒学会 Golang 片段 – Zip

Creates a new slice out of the two supplied by creating each possible pair from the collections.

Use reflect.ValueOf() to get the arrays or slices, Value.Len() and make() to create an appropriate slice for the result.
Use a for loop with Value.Len(), Value.Index() and Value.Interface() to populate the result.

继续阅读 30秒学会 Golang 片段 – XProduct

Returns true if all elements in the collection have the same value, false otherwise.

Use reflect.ValueOf() to get the array or slice, Value.Index() and Value.Interface() to get the first value.
Use a for loop with Value.Len() and Value.Index() to iterate over each element and compare it to the search value.
Return false if a non-matching value is found, true otherwise.

继续阅读 30秒学会 Golang 片段 – AllSame

Returns a map with index-value pairs.

Use reflect.ValueOf() to get the array or slice, make() to create a new map.
Use a for loop with Value.Len(), Value.Index() and Value.Interface() to iterate over each element and add it to the map.

继续阅读 30秒学会 Golang 片段 – WithIndex

Returns true if the given element can be found in the collection, false otherwise.

Use reflect.ValueOf() to get the array or slice and the value to search for.
Use a for loop with Value.Len() and Value.Index() to iterate over each element and compare it to the search value.
Return true if a matching value is found, false otherwise.

继续阅读 30秒学会 Golang 片段 – Includes

Returns the first index at which a given element can be found in the collection, or -1 if it is not present.

Use reflect.ValueOf() to get the array or slice and the value to search for.
Use a for loop with Value.Len() and Value.Index() to iterate over each element and compare it to the search value.
Return the index if a matching value is found, -1 otherwise.

继续阅读 30秒学会 Golang 片段 – IndexOf