I suspect it's because your reference to the time_array is actually copying it 
into your closure that's running as the goroutine instead of making a reference 
to it. You might do better to pass that array as a parameter to the goroutine 
instead of trying to absorb it as context.

I should note that your approach of filling a statically sized array is 
probably not the best one, because that's going to eventually panic once you 
hit the limit.  But that's a bit out of scope of this discussion, and I'm sure 
you mostly meant it for illustrative purposes.


- Dave


> On Feb 16, 2019, at 9:29 AM, Hemant Singh <hemanti...@gmail.com> wrote:
> 
> I have the following program.  The program is processing network packets at 1 
> Gbps rate.  I don't want to print the rate for every packet.  Instead, I'd 
> like to save the rate in an array and dump the array on Ctrl-c of the 
> program.  When I dump the array on Ctrl-c, the data is all zeroes.  Please 
> see "<===" in the program below.  Anything else one could do?
> 
> Thanks.  
> 
> -Hemant
> 
> func main() {
> 
>   const SZ = 65536
>   var time_array [SZ]float64
> 
>   c := make(chan os.Signal)
>   signal.Notify(c, os.Interrupt, syscall.SIGTERM)
> 
>   go func() {
>      <-c
>      fmt.Printf("You pressed ctrl + C. User interrupted infinite loop.")
>      fmt.Printf("%v ", time_array);  <=== all zeroes printed. 
>      os.Exit(0)
>   }()
> 
> 
>   i := 0
>   for {
>     ...
>     time_array[i] = rate
>     fmt.Printf("%f bps ", rate) <=== value printed is correct. 
>     i++
>   }
> }

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to