Have you considered this?
```go
func IsBetween(value, min, max int) bool {
   return value>=min && value <=max
}

if IsBetween(len(myMap), 10, 25) {
  //do something
}
```

On Friday, March 18, 2022 at 11:20:37 AM UTC+7 RussellLuo wrote:

> Hi there,
>
> Thanks to Go generics in 1.18, I can write a generic function `LenBetween` 
> for a slice:
>
> ```go
> func SliceLenBetween[T ~[]E, E any](s T, min, max int) bool {
>         return len(s) >= min && len(s) <= max
> }
> ```
>
> as well as for a map:
>
> ```go
> func MapLenBetween[T map[K]V, K comparable, V any](s T, min, max int) bool 
> {
>         return len(s) >= min && len(s) <= max
> }
> ```
>
> Is there any way to write a constraint, say, SliceOrMap, to support either 
> a slice or a map?
>
> With the help of SliceOrMap, then I can write a more generic version 
> `LenBetween` like this:
>
> ```go
> func MapLenBetween[T SliceOrMap](s T, min, max int) bool {
>         return len(s) >= min && len(s) <= max
> }
> ```
>
> Thanks in advance!
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2e9fc7bc-0b70-4d15-8469-c600566fbb14n%40googlegroups.com.

Reply via email to