Created
August 31, 2022 15:26
-
-
Save project0/8e7759895d6286cf8499e6fcf3dd8cc3 to your computer and use it in GitHub Desktop.
yeagi waitgroups
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
package main | |
import ( | |
"github.com/traefik/yaegi/interp" | |
"github.com/traefik/yaegi/stdlib" | |
) | |
const code = ` | |
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func worker(id int) { | |
fmt.Printf("Worker %d starting\n", id) | |
time.Sleep(time.Second) | |
fmt.Printf("Worker %d done\n", id) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
for i := 1; i <= 5; i++ { | |
wg.Add(1) | |
i := i | |
go func() { | |
defer wg.Done() | |
worker(i) | |
}() | |
} | |
wg.Wait() | |
} | |
` | |
func main() { | |
i := interp.New(interp.Options{}) | |
i.Use(stdlib.Symbols) | |
_, err := i.Eval(code) | |
if err != nil { | |
panic(err) | |
} | |
_, err = i.Eval(`fmt.Println("Hello Yaegi")`) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment