Hey folks, I was working on a project and felt my error handling was too cryptic, so I was hoping to use error wrapping (%w in fmt.Errorf) to give me the context and error chain that I need. But from what I understand it can be quite slow because of the string allocation made for fmt.Errorf? What are your thoughts on this? Do you know some better way? Please let me know, thanks in advance peeps
#Is error wrapping efficient?
4 messages · Page 1 of 1 (latest)
String allocation here should not be noticeable in often, unless your program is error driven, which should be avoid of.
If you do worry about such, you can create your own error type and implement error interface to only construct the string when necessary. Be sure also implement Unwrap() method as mentioned in errors
feelings on performance are usually baseless and almost never reflect reality. if you really think it's an issue, benchmark and measure. regardless it's typically not the happy path that an error is being produced so I don't concern myself with it nearly as much anyway
Thanks, ill try this out