Very much agree, but also something that has not been explicitly (or at
least deeply) said here is the use of a tiny type (when the number of
uses warrants - this is salt to taste).

type set map[T]struct{}

func (s set) has(v T) bool {
        _, ok := s[v]
        return ok
}

func (s set) add(v T) { s[v] = struct{}{} }

This gets you the expressiveness that's needed, doesn't expose what some
people think is ugly and avoids the ternary state problem of map[T]bool.


On Tue, 2016-07-26 at 12:27 -0700, Nate Finch wrote:
> I much prefer struct{}.  For gophers, it is very expressive of "nothing to 
> see here".    This value has no meaning, so don't even bother with it.
> 
> And as someone else said, the _, ok pattern is so pervasive, it's no harder 
> for me to read than if myMap[foo] { 
> 
> if _, ok := myMap[foo]; ok {
> 
> }
> 
> I think it tells the story better than bool.  If you use bool, you're 
> saying "this value could be true or false", when in fact, the value should 
> always be true, or not exist.


-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to