英文:
capture panic into string variable
问题
我有一个不寻常的请求。我需要一种将我捕获的 panic 转换为字符串的方法,以便我可以保存它。我使用 recover() 捕获了 panic,但它无法转换为字符串,因为它是一个 runtime.boundError 类型的接口。有人知道如何将 panic 保存到字符串变量中以供将来使用吗?
英文:
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?
答案1
得分: 1
我认为你可以使用类型断言
。使用类型断言,你可以检查一个接口(在这种情况下是runtime.BoundError)是否是特定类型的。通常用于确保接口值满足另一个接口或找到接口的具体类型。
你可以查看这个示例代码,将其断言为string
类型。
https://go.dev/play/p/b6EtGrh0gGj
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论