package main import ( "fmt" "slices" "strings" "sync" ) // guestList holds names. Make one with newGuestList. type guestList struct { mu sync.Mutex // mu protects names names []string } func newGuestList() *guestList { return &guestList{} } func (g *guestList) add(name string) { g.mu.Lock() defer g.mu.Unlock() g.names = append(g.names, name) } func (g *guestList) size() int { g.mu.Lock() defer g.mu.Unlock() return len(g.names) } func (g *guestList) sorted() []string { g.mu.Lock() defer g.mu.Unlock() names := slices.Clone(g.names) slices.Sort(names) return names } func main() { tables := [][]string{ {"Rina", "Budi", "Sari"}, {"Keiko", "Arif"}, {"Maya", "Tom", "Ana", "Joko"}, } guests := newGuestList() var wg sync.WaitGroup for _, table := range tables { wg.Go(func() { for _, name := range table { guests.add(name) } }) } wg.Wait() fmt.Println("Guests:", guests.size()) fmt.Println(strings.Join(guests.sorted(), ", ")) }