Created
June 13, 2015 04:37
-
-
Save paulmooring/fbff022a1756c69c0081 to your computer and use it in GitHub Desktop.
concurrency problem with golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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