clarkk
clarkk

Reputation: 27689

delete unassigned (zero values) in a slice

How to delete all zero values or unassigned values?

Here a stack trace is put into a slice.. How can all the unassigned (zero values) be deleted?

Is there some fancy function to slice a slice.. Something like substring of a string

trace := make([]byte, 1024)
runtime.Stack(trace, true)

Upvotes: 0

Views: 775

Answers (1)

Simon Fox
Simon Fox

Reputation: 6425

Use a slice expression to trim the unused portion of the stack buffer. The Stack function conveniently returns the number of bytes written to the buffer.

    trace := make([]byte, 1024)
    n := runtime.Stack(trace, true)
    trace = trace[:n]

playground link

Upvotes: 1

Related Questions