go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/blogctl/pkg/aws/s3/set.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package s3
     9  
    10  import "sync"
    11  
    12  // Set is a set that can be accessed concurrently.
    13  type Set struct {
    14  	syncroot sync.Mutex
    15  	values   map[string]struct{}
    16  }
    17  
    18  // Set adds a value
    19  func (s *Set) Set(value string) {
    20  	s.syncroot.Lock()
    21  	if s.values == nil {
    22  		s.values = make(map[string]struct{})
    23  	}
    24  	s.values[value] = struct{}{}
    25  	s.syncroot.Unlock()
    26  }
    27  
    28  // Has returns if a value is in the set
    29  func (s *Set) Has(value string) (has bool) {
    30  	s.syncroot.Lock()
    31  	if s.values == nil {
    32  		s.syncroot.Unlock()
    33  		return
    34  	}
    35  	_, has = s.values[value]
    36  	s.syncroot.Unlock()
    37  	return
    38  }