Returns the index of the first element in the provided collection that satisfies the provided testing function, -1 otherwise.

Implement an appropriate function for each type.
Use range to iterate over elements in the given collection, using f to determine if the element matches.
Return the index of the first matching element or -1 if none is found.

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

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