{"id":1202,"date":"2013-05-06T09:24:40","date_gmt":"2013-05-06T15:24:40","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1202"},"modified":"2018-06-30T22:24:33","modified_gmt":"2018-07-01T04:24:33","slug":"google-go-recover-example","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2013\/05\/google-go-recover-example\/","title":{"rendered":"Google Go: recover() example"},"content":{"rendered":"<p>Here&#8217;s an example of how to use recover() in Google Go.<\/p>\n<pre class=\"prettyprint lang-go linenums\">\r\npackage main\r\n\r\nimport \"fmt\"\r\n\r\nfunc gandalf( doTalkTo bool ){\r\n\tdefer catch()\r\n\tif(doTalkTo){\r\n\t\t\/\/ Panic stops execution of the current function and stops unwinding the stack, calling an deferred functions along the way.\r\n\t\tpanic(\"You shall not pass!\")\t\r\n\t}\r\n}\r\n\/\/ catch() must be called as a deferred \r\nfunc catch(){\r\n\t\/\/ recover() regains control over the program execution when panic() is called.\r\n\t\/\/ recover() returns is the argument passed from panic()\r\n\tif r := recover(); r != nil {\r\n\t\tfmt.Println(\"Error:\", r)\r\n\t} else {\r\n\t\tfmt.Println(\"No problems occurred\")\r\n\t}\r\n}\r\nfunc main() {\r\n\tgandalf(true)\r\n\tgandalf(false)\r\n\t\/\/unwinds to the top of the stack and ends the program\r\n\tpanic(\"Where am I going?\");\r\n}\r\n<\/pre>\n<p><\/code><br \/>\n<code><\/p>\n<pre>\r\nOutput:\r\nError: You shall not pass!\r\nNo problems occurred\r\npanic: Where am I going?\r\n\r\ngoroutine 1 [running]:\r\nmain.main()\r\n<\/pre>\n<p>Demo: http:\/\/play.golang.org\/p\/xu9Jd1YI0T<\/p>\n<p>More information here:<br \/>\n<a href=\"http:\/\/golang.org\/doc\/effective_go.html#recover\">Effective Go - The Program Language<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example of how to use recover() in Google Go. package main import &#8220;fmt&#8221; func gandalf( doTalkTo bool ){ defer catch() if(doTalkTo){ \/\/ Panic stops execution of the current function and stops unwinding the stack, calling an deferred functions along the way. panic(&#8220;You shall not pass!&#8221;) } } \/\/ catch() must be called as &hellip; <a href=\"https:\/\/bateru.com\/news\/2013\/05\/google-go-recover-example\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Google Go: recover() example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[20,156,155,251,16],"class_list":["post-1202","post","type-post","status-publish","format-standard","hentry","category-backend-tech","tag-demo","tag-golang","tag-google-go-2","tag-recovery","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"predecessor-version":[{"id":1534,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1202\/revisions\/1534"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}