Restart Go program by itself.

1,334 views
Skip to first unread message

Jiahua Chen

unread,
May 13, 2013, 11:41:12 PM5/13/13
to golan...@googlegroups.com
My friend asked me if there is anyway to avoid all track information after the program crashed, because it print hundreds of lines of track information if the program is large enough, finally we found out that we can use recover() to achieve this, and also, we found there is a approach to restart program by itself, I post here in case someone may not know yet.

func mymain() {   
    defer func() {
        recover()   
   }()
    ......// your code that was in real main().
}

func main() {
    for {
        mymain()   
   }
}

Dave Cheney

unread,
May 13, 2013, 11:50:21 PM5/13/13
to Jiahua Chen, golang-nuts
Sadly this won't work

http://play.golang.org/p/Kcps61WZH9
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jiahua Chen

unread,
May 13, 2013, 11:53:41 PM5/13/13
to golan...@googlegroups.com, Jiahua Chen
It seems doesn't work with goroutine, but this one is fine. http://play.golang.org/p/-p-NbIjCO4

Dave Cheney

unread,
May 13, 2013, 11:57:36 PM5/13/13
to Jiahua Chen, golang-nuts
If you are going to recommend this solution, then at least log the
reason for the panic. It will be especially important when people
contacting you for support when their process is looping because of
panic("out of memory")

Jiahua Chen

unread,
May 13, 2013, 11:59:44 PM5/13/13
to golan...@googlegroups.com, Jiahua Chen
Good advice! I'll forward to him.
Reply all
Reply to author
Forward
0 new messages