gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.82k stars 265 forks source link

Issues when using sync package #120

Closed zbisch closed 6 years ago

zbisch commented 6 years ago

Greetings, I just started using gophernotes, so my apologies if this issue is already known (I did try searching through previous issues for anything similar). Also, this issue may be one upstream with gomacro, though I couldn't find anything there either.

In my code, I was trying to use sync.WaitGroup to synchronize my worker pools before moving on to the next phase, but ran into some strange errors. Here's a simplified example of the troubles I was having:

import (
    "fmt"
    "sync"
    "time"
)

func thingOne(wg *sync.WaitGroup) {
    fmt.Println("Hi from 1")
    wg.Done()
}

func thingTwo(wg *sync.WaitGroup) {
    fmt.Println("Hi from 2")
    wg.Done()
}

func test(){
    var wg sync.WaitGroup
    wg.Add(2)
    go thingOne(&wg)
    go thingTwo(&wg)
    fmt.Println("Waiting for all goroutines to exit")
    wg.Wait()
    fmt.Println("They're done")
}

The error message I get is: repl.go:9:3: type sync.WaitGroup has no field or method "Done": wg.Done

If I remove the calls to Done() in the two goroutines, then I get the error below: repl.go:17:2: not a package: "wg" in wg.Add <*ast.SelectorExpr>

I was able to build/run the code using the standard Go compiler.

cosmos72 commented 6 years ago

Yes, it's a bug in the upstream gomacro. Thanks for the report, I fixed this issue in b632224d66228467119da53a6fb81365ca1b8551

You may want to know that the following does not work yet:

f := (*sync.WaitGroup).Done

i.e. extracting a function func(*<receiver>, ...) ... from a method with pointer receiver. It's a known bug, I will fix it when I have a little time.

As a workaround, the following instead works in gophernotes - even though it's not accepted by Go compilers:

f := sync.WaitGroup.Done