1

Is there any method to get a stack trace as a string?

Looking at the debug package (https://golang.org/pkg/runtime/debug/) it can only be printed to standard output.

1 Answer 1

5

runtime.Stack() puts a formatted stack trace into a supplied []byte. You can then convert that to a string.

You can also use debug.Stack(), which allocates a large enough buffer to hold the entire stack trace, puts the trace in it using runtime.Stack, and returns the buffer ([]byte).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I should've looked at the source for PrintStack ( golang.org/src/runtime/debug/stack.go ). The function debug.Stack relies on runtime.Stack but catpures the entire buffer. I think that should ideally be used. Can you edit your answer, and I'll mark it as such?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.