This question already has an answer here:
Trying to solve the problem of reading phone numbers from a file (line by line) and hash them concurrently (md5) in go with a maximum of 100 concurrent process maximum, creating a fixed number of goroutines. tried using go pipeline and bounded parallelism but did not work. any guidance, please?
this is what I tried, https://play.golang.org/p/vp7s512l8w4
</div>
package main
import (
"bufio"
"fmt"
"os"
"sync"
)
func hashPhoneNumers(phoneNO string, ch chan struct{}, group *sync.WaitGroup) {
do_hash(phoneNO)
<-ch
group.Done()
}
func do_hash(s string) {
fmt.Println("do hash: ", s)
}
func main() {
c := make(chan struct{}, 100)
file, err := os.Open("file.txt")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
defer file.Close()
line := bufio.NewScanner(file)
wg := sync.WaitGroup{}
for line.Scan() {
// with a maximum of 100 concurrent process maximum
c <- struct{}{}
wg.Add(1)
go hashPhoneNumers(line.Text(), c, &wg)
}
// wait all goroutines done
wg.Wait()
close(c)
}
this works fine