Home > Software design >  capture panic into string variable
capture panic into string variable

Time:09-16

I have an unusual request. I need a way to convert the panic that I captured to string so I can save it. I have captured panic with recover() but it can't be converted to string because it is an interface that is runtime.boundError type. Does anyone know how can I save panics into string var for future use?

CodePudding user response:

I think you can use type assertion . using type assertion you can check if an interface (in this case runtime.BoundError as you said) is of a specific type . and it is generally used to make sure that an interface value satisfies another interface or to find the concrete type of the interface.

check this playground . you can assert it into string type . https://go.dev/play/p/b6EtGrh0gGj

  •  Tags:  
  • go
  • Related