gopherdata / gophernotes

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

expected declaration, found 'package' #60

Closed animesh closed 7 years ago

animesh commented 7 years ago

facing "expected declaration, found 'package' " error while trying to run https://play.golang.org/p/ZdFpbahgC1 :

// Go's `math/rand` package provides
// [pseudorandom number](http://en.wikipedia.org/wiki/Pseudorandom_number_generator)
// generation.

package main

import "time"
import "fmt"
import "math/rand"

func main() {

    // For example, `rand.Intn` returns a random `int` n,
    // `0 <= n < 100`.
    fmt.Print(rand.Intn(100), ",")
    fmt.Print(rand.Intn(100))
    fmt.Println()

    // `rand.Float64` returns a `float64` `f`,
    // `0.0 <= f < 1.0`.
    fmt.Println(rand.Float64())

    // This can be used to generate random floats in
    // other ranges, for example `5.0 <= f' < 10.0`.
    fmt.Print((rand.Float64()*5)+5, ",")
    fmt.Print((rand.Float64() * 5) + 5)
    fmt.Println()

    // The default number generator is deterministic, so it'll
    // produce the same sequence of numbers each time by default.
    // To produce varying sequences, give it a seed that changes.
    // Note that this is not safe to use for random numbers you
    // intend to be secret, use `crypto/rand` for those.
    s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)

    // Call the resulting `rand.Rand` just like the
    // functions on the `rand` package.
    fmt.Print(r1.Intn(100), ",")
    fmt.Print(r1.Intn(100))
    fmt.Println()

    // If you seed a source with the same number, it
    // produces the same sequence of random numbers.
    s2 := rand.NewSource(42)
    r2 := rand.New(s2)
    fmt.Print(r2.Intn(100), ",")
    fmt.Print(r2.Intn(100))
    fmt.Println()
    s3 := rand.NewSource(42)
    r3 := rand.New(s3)
    fmt.Print(r3.Intn(100), ",")
    fmt.Print(r3.Intn(100))
}

code runs fine via 'go run' command line, any ideas on how to get around this?

dwhitena commented 7 years ago

Hi @animesh. Sorry for the delay here. In gophernotes, you don't have to specify a package or function main. Just paste your various pieces of code into the notebook and go for it. That is, you can just past the following into respective cells:

import "time"
import "fmt"
import "math/rand"
    // For example, `rand.Intn` returns a random `int` n,
    // `0 <= n < 100`.
    fmt.Print(rand.Intn(100), ",")
    fmt.Print(rand.Intn(100))
    fmt.Println()

    // `rand.Float64` returns a `float64` `f`,
    // `0.0 <= f < 1.0`.
    fmt.Println(rand.Float64())

etc...

Let me know if you have any issues with this strategy.

animesh commented 7 years ago

Thanks Daniel for looking into this :+1:

animesh commented 7 years ago

 @dwhitena , error has disappeared, but i don't see any output though...

below is my system

uname -a
Linux animeshs-Latitude-E6230 4.10.0-20-generic #22-Ubuntu SMP Thu Apr 20 09:22:42 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

and the kernel configuration

cat .local/share/jupyter/kernels/gophernotes/kernel.json 
{
    "argv": [
        "gophernotes",
        "{connection_file}"
        ],
    "display_name": "Go",
    "language": "go",
    "name": "go"
}

any ideas what could be the issue?

dwhitena commented 7 years ago

@animesh can you provide the exact code you are running in the notebook cell?

animesh commented 7 years ago

import "time" import "fmt" import "math/rand" fmt.Print(rand.Intn(100), ",") fmt.Print(rand.Intn(100)) fmt.Println() fmt.Println(rand.Float64())

dwhitena commented 7 years ago

Hmmm... Sorry you are experiencing this. This is what I get:

image

Maybe you are using an old release of gophernotes? Do you know what version?

animesh commented 7 years ago

@dwhitena thanks for tip regarding version, i am not sure what worked, but reinstalling makes it work, sort of :+1: the issue now i am facing is that some sort of accumulation is going on when i call the cell again. For example, a run with code similar to your cell-2 gives

81,8747,5981,1825,40456,0694,1162,9728,4211,5237,6495,6528,847,7287,8790,5541,8387,1429,6737,1485,6413,0194,3433,778,4159,3957,1189,90,5888,8703,5451,0605,6266,8561,2783,6563,62,8447,4577,3996,023,5337,3341,5933,4391,278,3646,740,352,435,9825,5115,5787,3
<nil>
102
<nil>

1
<nil>
0.0005138155161213613
22
<nil>

while re-run of the same cell gives

81,8747,5981,1825,40456,0694,1162,9728,4211,5237,6495,6528,847,7287,8790,5541,8387,1429,6737,1485,6413,0194,3433,778,4159,3957,1189,90,5888,8703,5451,0605,6266,8561,2783,6563,62,8447,4577,3996,023,5337,3341,5933,4391,278,3646,740,352,435,9825,5115,5787,1010,3
<nil>
852
<nil>

1
<nil>
0.39998376285699544
20
<nil>

any ideas what is going on?

dwhitena commented 7 years ago

@animesh I think this is the expected behavior. Closing for now.

Basically, when you rerun the cell, you are seeing new random numbers being generated. Also, you can try this in v1 (RC just released). It should behave as expected. Please follow up here if you see otherwise.

animesh commented 7 years ago

@dwhitena :+1: works nice, thx :)