Doc examples for new slices and maps packages

278 views
Skip to first unread message

ben...@gmail.com

unread,
Jun 21, 2023, 7:46:02 PM6/21/23
to golang-dev
Hi folks,

I notice there's no examples in the API docs for "slices" and "maps".

A lot of the functions are pretty obvious, like slices.Contains, slices.Clone, and maps.Keys. But the new Sort* functions, and other functions that use a new-style cmp function, would really benefit from a couple of simple examples. By contrast, the existing sort.Slice* functions have nice examples.

One other thing I noticed: slices.CompactFunc doesn't have quite enough info (and an example is probably helpful there, too). It says "CompactFunc is like Compact but uses a comparison function." But it's an equality function, not really a comparison (cmp) function, and either way, the doc comment doesn't explain how the function is used. Could be tweaked to "... but uses eq(a, b) to determine whether two elements are equal." Or even terser (like maps.EqualFunc): "... but compares values using eq." For comparison, slices.ContainsFunc's doc says what it does with its function.

In any case, I wanted to see if there was appetite for adding examples here -- if so, I'm happy to submit a patch.

-Ben

Ian Lance Taylor

unread,
Jun 21, 2023, 8:34:27 PM6/21/23
to ben...@gmail.com, golang-dev
Good examples are always welcome. Thanks.

Ian

ben...@gmail.com

unread,
Jun 22, 2023, 8:42:53 AM6/22/23
to golang-dev
Added examples, and a couple of doc comment tweaks:

I may have gone overboard with the slices examples, but I figure with generics function signatures being somewhat complex, an example one most of these is helpful.

-Ben
Reply all
Reply to author
Forward
0 new messages