From d10b82bb313eedfea8fd730c7875b47db98df6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Fri, 27 Oct 2023 09:22:06 +0200 Subject: [PATCH] add StrSet type --- sugar.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sugar.go b/sugar.go index 3238839..1c2b083 100644 --- a/sugar.go +++ b/sugar.go @@ -137,3 +137,32 @@ func CheckExit(err error) { os.Exit(1) } } + +/* + * Sets + */ + +// StrSet a set type for strings. +type StrSet map[string]struct{} + +// Add adds a new key to the set if it does not already exist. +func (s *StrSet) Add(str string) { + (*s)[str] = struct{}{} +} + +// Has checks if the given string is present in the set. +func (s *StrSet) Has(str string) bool { + if _, ok := (*s)[str]; ok { + return true + } + return false +} + +// AddHas behaves like Add, but returns true if the key already existed. +func (s *StrSet) AddHas(str string) bool { + if ok := s.Has(str); ok { + return ok + } + s.Add(str) + return false +}