Returns the value of the last element in the provided collection that satisfies the provided testing function.

Implement an appropriate function for each type.
Use a for loop to iterate over elements in the given collection, using f to determine if the element matches.
Return the value of the last matching element and nil if a value is found.
Use fmt.Errorf() to generate an appropriate error and return it alongsie a zero value for the type.

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

Returns a map with the unique values of the collection as keys and their frequencies as the values.

Implement an appropriate function for each type.
Use make() to create a map.
Use range to iterate over the elements in the collection, adding to existing keys every time the same value is encountered.

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

Deduplicates the elements in a given array or slice.

Implement an appropriate function for each type.
Use make() to create a map and an appropriate slice.
Use range to iterate over the elements in the collection and the map to check if they have been seen before.
Use append() to add unique values to the slice.

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

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 the value of the first element in the provided collection that satisfies the provided testing function.

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 value of the first matching element and nil if a value is found.
Use fmt.Errorf() to generate an appropriate error and return it alongsie a zero value for the type.

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

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 a new slice with all elements that pass the test implemented by the provided function.

Implement an appropriate function for each type.
Use make() to create an appropriate slice.
Use range to iterate over elements in the given collection, using append() to add them to the resulting slice based on the result of fn.
The functions can be tweaked to allow for an index to be passed to fn as a second argument, if desired.

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

Returns true if all elements in the collection pass the test implemented by the provided function, false otherwise.

Implement an appropriate function for each type.
Use range to iterate over elements in the given collection, returning true or false based on the result of fn.
The functions can be tweaked to allow for an index to be passed to fn as a second argument, if desired.

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