foomo-docs/assets/js/a081292a.24413197.js
2023-04-21 21:42:08 +00:00

1 line
5.7 KiB
JavaScript

"use strict";(self.webpackChunkfoomo=self.webpackChunkfoomo||[]).push([[429],{1259:(e,t,n)=>{n.d(t,{X:()=>d});var o=n(7462),a=n(2801),i=n(7294),r=n(4991),c=n(9030),l=n(3746),s=n(9960);const d=e=>{const[t,n]=(0,i.useState)(!1),[d,p]=(0,i.useState)(""),m=(0,a.r)();(0,i.useEffect)((()=>{""==d&&""!==e.id&&fetch("https://docs.bestbytes.com/services/playground?id="+e.id,{mode:"cors"}).then((e=>e.text().then((e=>p(e))))).catch((e=>p("can not load source yet")))}),[d]);const u="https://goplay.tools/snippet/"+e.id;return i.createElement(i.Fragment,null,i.createElement("div",{style:{paddingBottom:"1rem",width:"100%"}},t?i.createElement(i.Fragment,null,i.createElement("button",{className:"button button--primary",onClick:e=>{n(!1)}},"close playground")," ",i.createElement("button",{className:"button button--primary"+(t?"":" disabled"),onClick:e=>{m.enter()}},"fullscreen")," "):i.createElement("button",{className:"button button--primary",onClick:e=>{n(!0)}},"open playground")," ",i.createElement(s.Z,{href:u},"go to ",u)),!t&&i.createElement(l.ZP,(0,o.Z)({},l.lG,{code:d,language:"go"}),(e=>{let{className:t,style:n,tokens:o,getLineProps:a,getTokenProps:r}=e;return i.createElement("pre",{className:t,style:n},""==d?"... loading sources":"",o.map(((e,t)=>i.createElement("div",a({line:e,key:t}),e.map(((e,t)=>i.createElement("span",r({token:e,key:t}))))))))})),i.createElement("div",{style:{display:t?"block":"none"}},i.createElement(c.k,{topic:"load external go playground, with all it\xb4s potentially evil cookies coming from https://goplay.tools",id:"goPlaygroundIsCool"},i.createElement("div",null,i.createElement(a.I,{handle:m},i.createElement(r.h,{src:u,proportion:e.proportion}))))))}},4991:(e,t,n)=>{n.d(t,{h:()=>a});var o=n(7294);const a=e=>{let{proportion:t,src:n,style:a}=e;return t||(t=4/3),a&&(a={}),o.createElement("div",{style:{width:"100%",height:0,paddingTop:100/t+"%",position:"relative",float:"left",...a}},o.createElement("iframe",{style:{width:"100%",height:"100%",display:"block",position:"absolute",top:0,left:0},src:n,frameBorder:"0",scrolling:"no",allowFullScreen:!0}))}},9030:(e,t,n)=>{n.d(t,{k:()=>i});var o=n(7294);const a="undefined"==typeof localStorage,i=e=>{const[t,n]=(0,o.useState)((i=e.id,"undefined"!=typeof localStorage&&localStorage.getItem(i)));var i;return(0,o.useEffect)((()=>{console.log("well it is cool",e.id,{isCool:t,SSR:a})}),[t,a]),t?e.children:o.createElement("div",null,o.createElement("button",{className:"button button--lg button--secondary",onClick:t=>{localStorage.setItem(e.id,"yes"),n(!0)}},e.topic))}},5280:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>s,contentTitle:()=>c,default:()=>u,frontMatter:()=>r,metadata:()=>l,toc:()=>d});var o=n(7462),a=(n(7294),n(3905)),i=n(1259);const r={title:"Panic and Recover",sidebar_position:2,tags:["Go Basics"]},c="Panic and Recover",l={unversionedId:"backend/go-by-example/panic-and-recover",id:"backend/go-by-example/panic-and-recover",title:"Panic and Recover",description:"Panic",source:"@site/docs/backend/go-by-example/panic-and-recover.mdx",sourceDirName:"backend/go-by-example",slug:"/backend/go-by-example/panic-and-recover",permalink:"/docs/backend/go-by-example/panic-and-recover",draft:!1,editUrl:"https://github.com/foomo/foomo-docs/tree/main/foomo/docs/backend/go-by-example/panic-and-recover.mdx",tags:[{label:"Go Basics",permalink:"/docs/tags/go-basics"}],version:"current",sidebarPosition:2,frontMatter:{title:"Panic and Recover",sidebar_position:2,tags:["Go Basics"]},sidebar:"backendSidebar",previous:{title:"Defer",permalink:"/docs/backend/go-by-example/defer"},next:{title:"Maps",permalink:"/docs/backend/go-by-example/nil-maps"}},s={},d=[{value:"Panic",id:"panic",level:2},{value:"Panic and Defer",id:"panic-and-defer",level:2},{value:"Panic and Recover",id:"panic-and-recover-1",level:2}],p={toc:d},m="wrapper";function u(e){let{components:t,...n}=e;return(0,a.kt)(m,(0,o.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,a.kt)("h1",{id:"panic-and-recover"},"Panic and Recover"),(0,a.kt)("h2",{id:"panic"},"Panic"),(0,a.kt)("p",null,(0,a.kt)("inlineCode",{parentName:"p"},"panic")," is used whenever our application can no longer function. In Go it's mostly left up to the developer to decide when an error is a problem in your application.\n",(0,a.kt)("inlineCode",{parentName:"p"},"panic")," helps you tell your program it encountered a fatal problem which can not be recovered. Panic always prints a stack trace to help locate the problematic line of code."),(0,a.kt)("h2",{id:"panic-and-defer"},"Panic and Defer"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-go"},'func main() {\n fmt.Println("Start")\n defer fmt.Println("Deferred")\n panic("Panic")\n fmt.Println("End")\n}\n')),(0,a.kt)("p",null,"The above code will print: 'Start', 'Deferred' and then 'Panic'. This is because defer statements will always be executed even in case of a panic.\nSo the order of execution is: Execute called function, then execute defer statements, and in case of a panic program execution will stop.\nSo even if your application will panic defer statements are handled and therefore resources will be closed accordingly."),(0,a.kt)("h2",{id:"panic-and-recover-1"},"Panic and Recover"),(0,a.kt)("p",null,"Try out the following example: "),(0,a.kt)(i.X,{id:"BN8Boz9LRVX",proportion:1.6,mdxType:"GoPlayground"}),"The function that panics will stop because it can no longer function so we don't see 'Is this printed?' printed. The recover function makes it possible to obtain the error in case a panic occurred, and leaves the decision how to proceed to the programmer. In this example, we decided to just log the error and continue to run. The function that panicked will stop execution at the panic, but the code that called it will continue to execute, which is why we see 'End' being printed.")}u.isMDXComponent=!0}}]);