you need to "make" your channels before using them.
> But, I don't know why. And the error message seems stop on
> "time.Sleep(20 * 1e9)" this line.
>
>
> package main // I try to run one goroutine after one goroutine
> import "time"
>
> func run(name string, sec int64, in_ch, out_ch chan int64) {
> <-in_ch
> for i:=0; i<10*1e5; i++ {
> time.Sleep(sec * 1e9)
> println(name)
> }
> out_ch<- sec
> }
>
> func main() {
> var c1,c2 chan int64
c1 := make(chan int64)
c2 := make(chan int64)
Thank you, it works.But, I don't know how to get this answer from the runtime error line.Does the error line point to the wrong line?I guess it should be minus one line.
Well, since it says that there is a nil channel being sent on, its fairly obvious what the problem is in this case to someone who knows they have to initialize a channel before using it.
More generally, the error says that you are sending on a nil chan. That means you want to look for a channel send operation when the channel isn't initialized. To find out which channel isn't initialized, you'd have to look at the stack trace (the part that's printed after the panic line). For example, here's the trace from golang.org/doc/play:runtime.panic+0xa7 /sandbox/go/src/pkg/runtime/proc.c:1023 runtime.panic(0x424744, 0xf840001210) runtime.panicstring+0xa3 /sandbox/go/src/pkg/runtime/runtime.c:92 runtime.panicstring(0x464753, 0x28) runtime.chansend1+0x1a /sandbox/go/src/pkg/runtime/chan.c:398 runtime.chansend1(0x0, 0x1) main.main+0xc7 /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:18 main.main() runtime.mainstart+0xf /sandbox/go/src/pkg/runtime/amd64/asm.s:77 runtime.mainstart() runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149 runtime.goexit() goroutine 3 [1]: main.run /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:4 main.run(0x41c4c8, 0x5, 0x3, 0x0, 0x0, ...) runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149 runtime.goexit() goroutine 2 [1]: main.run /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:4 main.run(0x41c2e0, 0x4, 0x2, 0x0, 0x0, ...) runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149 runtime.goexit()You can see that you have the two extra goroutines you started that haven't really started running yet. The interesting part is the first goroutine. Ignore the lines in package runtime (they're not your code). The top (and only) stack frame in your code is main.main, and it shows where in the function the program was when the error occurred. Here its says its at line 18 of prog.go (which is just where goplay saves your code to compile it). That line is ` c1<- 1`, which tells you that c1 needs to be initialized. So you look back and see where the value comes from in order to fix the problem. While checking, you may also notice you have to fix c2, or you might not notice until the program complains.
lucky(/tmp) % 6g prog.go && 6l prog.6 && ./6.out 2>&1 | head -n2
waiting...
panic: runtime error: send to nil channel
runtime.chansend1+0x1a /Users/dave/go/src/pkg/runtime/chan.c:398
runtime.chansend1(0x0, 0x1)
main.main+0xc7 /private/tmp/prog.go:18
18: c1<- 1
What release OS and arch are you using ?
On Wed, Mar 2, 2011 at 11:51 AM, Daniel Lin <dli...@gmail.com> wrote:
> Thank you, it works.
> But, I don't know how to get this answer from the runtime error line.
> Does the error line point to the wrong line?
> I guess it should be minus one line.
> 2011/3/2 roger peppe <rogp...@gmail.com>
>>
Works fine for me,
lucky(/tmp) % 6g prog.go && 6l prog.6 && ./6.out 2>&1 | head -n2
waiting...
panic: runtime error: send to nil channelruntime.chansend1+0x1a /Users/dave/go/src/pkg/runtime/chan.c:398
runtime.chansend1(0x0, 0x1)
main.main+0xc7 /private/tmp/prog.go:18
18: c1<- 1
What release OS and arch are you using ?
Channels must be created :-)
c1 = make(chan int64)
c2 = make(chan int64)
--
Best regards,
santucco