February 2018
Intermediate to advanced
340 pages
9h 43m
English
package main import ( "fmt" "sync" ) var names = []interface{}{"Alan", "Joe", "Jack", "Ben", "Ellen", "Lisa", "Carl", "Steve", "Anton", "Yo"} type Source struct { m *sync.Mutex o *sync.Once data []interface{} } func (s *Source) Pop() (interface{}, error) { s.m.Lock() defer s.m.Unlock() s.o.Do(func() { s.data = names fmt.Println("Data has been loaded.") }) if len(s.data) > 0 { res := s.data[0] s.data = s.data[1:] return res, nil } return nil, fmt.Errorf("No data available") } func main() { s := &Source{&sync.Mutex{}, &sync.Once{}, nil} wg := &sync.WaitGroup{} wg.Add(10) for i := 0; i < ...Read now
Unlock full access