Skip to content

Instantly share code, notes, and snippets.

@paulmooring
Created June 13, 2015 04:37
Show Gist options
  • Save paulmooring/fbff022a1756c69c0081 to your computer and use it in GitHub Desktop.
Save paulmooring/fbff022a1756c69c0081 to your computer and use it in GitHub Desktop.
concurrency problem with golang
files, err := ioutil.ReadDir("./plugins/")
if err != nil {
panic(err)
}
var plugin_group sync.WaitGroup
plugin_group.Add(len(files))
for _, f := range files {
fmt.Println("Found: " + f.Name())
go func() {
defer plugin_group.Done()
content, err := ioutil.ReadFile("./plugins/" + f.Name())
if err != nil {
panic(err)
}
fmt.Println(string(content))
}()
}
plugin_group.Wait()
/*
$ cat plugins/test.rb
puts "testing mruby"
$ cat plugins/test2.rb
"do a thing"
$ cat plugins/test3.rb
1 + 4
Output:
Found: test.rb
Found: test2.rb
Found: test3.rb
1 + 4
1 + 4
1 + 4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment