File: main.go

package info (click to toggle)
golang-github-fatih-set 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 3
file content (38 lines) | stat: -rw-r--r-- 646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
	"log"
	"strconv"
	"sync"

	"github.com/fatih/set"
)

func main() {
	log.Print("Thread safe set operations")

	log.Print("Define wait group for waiting on goroutines")
	var wg sync.WaitGroup

	log.Print("Initialize our thread safe Set")
	s := set.New(set.ThreadSafe)

	log.Print("Add items concurrently (item1, item2, and so on)")
	for i := 0; i < 10; i++ {
		wg.Add(1)

		go func(i int) {
			defer wg.Done()

			item := "item" + strconv.Itoa(i)
			log.Print("adding " + item)
			s.Add(item)
		}(i)
	}

	log.Print("Wait until all concurrent calls finished and print our set")
	wg.Wait()
	log.Print(s)

	log.Print("Done")
}